From Fulko Hew via https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=4615 :
[obnox/wireshark/wip.git] / epan / dissectors / packet-snmp.c
1 /* Do not modify this file.                                                   */
2 /* It is created automatically by the ASN.1 to Wireshark dissector compiler   */
3 /* packet-snmp.c                                                              */
4 /* ../../tools/asn2wrs.py -b -p snmp -c ./snmp.cnf -s ./packet-snmp-template -D . snmp.asn */
5
6 /* Input file: packet-snmp-template.c */
7
8 #line 1 "packet-snmp-template.c"
9 /* packet-snmp.c
10  * Routines for SNMP (simple network management protocol)
11  * Copyright (C) 1998 Didier Jorand
12  *
13  * See RFC 1157 for SNMPv1.
14  *
15  * See RFCs 1901, 1905, and 1906 for SNMPv2c.
16  *
17  * See RFCs 1905, 1906, 1909, and 1910 for SNMPv2u [historic].
18  *
19  * See RFCs 2570-2576 for SNMPv3
20  * Updated to use the asn2wrs compiler made by Tomas Kukosa
21  * Copyright (C) 2005 - 2006 Anders Broman [AT] ericsson.com
22  *
23  * See RFC 3414 for User-based Security Model for SNMPv3
24  * See RFC 3826 for  (AES) Cipher Algorithm in the SNMP USM
25  * See RFC 2578 for Structure of Management Information Version 2 (SMIv2)
26  * Copyright (C) 2007 Luis E. Garcia Ontanon <luis@ontanon.org>
27  *
28  * $Id$
29  *
30  * Wireshark - Network traffic analyzer
31  * By Gerald Combs <gerald@wireshark.org>
32  * Copyright 1998 Gerald Combs
33  *
34  * Some stuff from:
35  *
36  * GXSNMP -- An snmp mangament application
37  * Copyright (C) 1998 Gregory McLean & Jochen Friedrich
38  * Beholder RMON ethernet network monitor,Copyright (C) 1993 DNPAP group
39  *
40  * This program is free software; you can redistribute it and/or
41  * modify it under the terms of the GNU General Public License
42  * as published by the Free Software Foundation; either version 2
43  * of the License, or (at your option) any later version.
44  *
45  * This program is distributed in the hope that it will be useful,
46  * but WITHOUT ANY WARRANTY; without even the implied warranty of
47  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
48  * GNU General Public License for more details.
49  *
50  * You should have received a copy of the GNU General Public License
51  * along with this program; if not, write to the Free Software
52  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
53  */
54
55 #define D(args) do {printf args; fflush(stdout); } while(0)
56
57 #ifdef HAVE_CONFIG_H
58 # include "config.h"
59 #endif
60
61 #include <string.h>
62 #include <ctype.h>
63
64 #include <glib.h>
65
66 #include <epan/packet.h>
67 #include <epan/strutil.h>
68 #include <epan/conversation.h>
69 #include <epan/etypes.h>
70 #include <epan/prefs.h>
71 #include <epan/sminmpec.h>
72 #include <epan/emem.h>
73 #include <epan/next_tvb.h>
74 #include <epan/uat.h>
75 #include <epan/asn1.h>
76 #include "packet-ipx.h"
77 #include "packet-hpext.h"
78
79
80 #include "packet-ber.h"
81
82 #include "packet-snmp.h"
83
84 #include <epan/crypt/crypt-sha1.h>
85 #include <epan/crypt/crypt-md5.h>
86 #include <epan/expert.h>
87 #include <epan/report_err.h>
88 #include <epan/oids.h>
89
90
91 #ifdef HAVE_LIBGCRYPT
92 #include <gcrypt.h>
93 #endif
94
95 /* Take a pointer that may be null and return a pointer that's not null
96    by turning null pointers into pointers to the above null string,
97    and, if the argument pointer wasn't null, make sure we handle
98    non-printable characters in the string by escaping them. */
99 #define SAFE_STRING(s, l)       (((s) != NULL) ? format_text((s), (l)) : "")
100
101 #define PNAME  "Simple Network Management Protocol"
102 #define PSNAME "SNMP"
103 #define PFNAME "snmp"
104
105 #define UDP_PORT_SNMP           161
106 #define UDP_PORT_SNMP_TRAP      162
107 #define TCP_PORT_SNMP           161
108 #define TCP_PORT_SNMP_TRAP      162
109 #define TCP_PORT_SMUX           199
110 #define UDP_PORT_SNMP_PATROL 8161
111
112 /* Initialize the protocol and registered fields */
113 static int proto_snmp = -1;
114 static int proto_smux = -1;
115
116 static gboolean display_oid = TRUE;
117 static gboolean snmp_var_in_tree = TRUE;
118
119 static gboolean snmp_usm_auth_md5(snmp_usm_params_t* p, guint8**, guint*, gchar const**);
120 static gboolean snmp_usm_auth_sha1(snmp_usm_params_t* p, guint8**, guint*, gchar const**);
121
122 static tvbuff_t* snmp_usm_priv_des(snmp_usm_params_t*, tvbuff_t*, gchar const**);
123 static tvbuff_t* snmp_usm_priv_aes(snmp_usm_params_t*, tvbuff_t*, gchar const**);
124
125
126 static void snmp_usm_password_to_key_md5(const guint8 *password, guint passwordlen, const guint8 *engineID, guint engineLength, guint8 *key);
127 static void snmp_usm_password_to_key_sha1(const guint8 *password, guint passwordlen, const guint8 *engineID, guint engineLength, guint8 *key);
128
129
130 static snmp_usm_auth_model_t model_md5 = {snmp_usm_password_to_key_md5, snmp_usm_auth_md5, 16};
131 static snmp_usm_auth_model_t model_sha1 = {snmp_usm_password_to_key_sha1, snmp_usm_auth_sha1, 20};
132
133 static const value_string auth_types[] = {
134         {0,"MD5"},
135         {1,"SHA1"},
136         {0,NULL}
137 };
138 static snmp_usm_auth_model_t* auth_models[] = {&model_md5,&model_sha1};
139
140
141 static const value_string priv_types[] = {
142         {0,"DES"},
143         {1,"AES"},
144         {0,NULL}
145 };
146 static snmp_usm_decoder_t priv_protos[] = {snmp_usm_priv_des, snmp_usm_priv_aes};
147
148 static snmp_ue_assoc_t* ueas = NULL;
149 static guint num_ueas = 0;
150 static snmp_ue_assoc_t* localized_ues = NULL;
151 static snmp_ue_assoc_t* unlocalized_ues = NULL;
152 /****/
153
154 /* Variabled used for handling enterprise spesific trap types */
155 typedef struct _snmp_st_assoc_t {
156         char *enterprise;
157         guint trap;
158         char *desc;
159 } snmp_st_assoc_t;
160 static guint num_specific_traps = 0;
161 static snmp_st_assoc_t *specific_traps = NULL;
162 static const char *enterprise_oid = NULL;
163 static guint generic_trap = 0;
164
165
166 static snmp_usm_params_t usm_p = {FALSE,FALSE,0,0,0,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,FALSE};
167
168 #define TH_AUTH   0x01
169 #define TH_CRYPT  0x02
170 #define TH_REPORT 0x04
171
172 /* desegmentation of SNMP-over-TCP */
173 static gboolean snmp_desegment = TRUE;
174
175 /* Global variables */
176
177 guint32 MsgSecurityModel;
178 tvbuff_t *oid_tvb=NULL;
179 tvbuff_t *value_tvb=NULL;
180
181 static dissector_handle_t snmp_handle;
182 static dissector_handle_t data_handle;
183
184 static next_tvb_list_t var_list;
185
186 static int hf_snmp_v3_flags_auth = -1;
187 static int hf_snmp_v3_flags_crypt = -1;
188 static int hf_snmp_v3_flags_report = -1;
189
190 static int hf_snmp_engineid_conform = -1;
191 static int hf_snmp_engineid_enterprise = -1;
192 static int hf_snmp_engineid_format = -1;
193 static int hf_snmp_engineid_ipv4 = -1;
194 static int hf_snmp_engineid_ipv6 = -1;
195 static int hf_snmp_engineid_cisco_type = -1;
196 static int hf_snmp_engineid_mac = -1;
197 static int hf_snmp_engineid_text = -1;
198 static int hf_snmp_engineid_time = -1;
199 static int hf_snmp_engineid_data = -1;
200 static int hf_snmp_decryptedPDU = -1;
201 static int hf_snmp_msgAuthentication = -1;
202
203 static int hf_snmp_noSuchObject = -1;
204 static int hf_snmp_noSuchInstance = -1;
205 static int hf_snmp_endOfMibView = -1;
206 static int hf_snmp_unSpecified = -1;
207
208 static int hf_snmp_integer32_value = -1;
209 static int hf_snmp_octetstring_value = -1;
210 static int hf_snmp_oid_value = -1;
211 static int hf_snmp_null_value = -1;
212 static int hf_snmp_ipv4_value = -1;
213 static int hf_snmp_ipv6_value = -1;
214 static int hf_snmp_anyaddress_value = -1;
215 static int hf_snmp_unsigned32_value = -1;
216 static int hf_snmp_unknown_value = -1;
217 static int hf_snmp_opaque_value = -1;
218 static int hf_snmp_nsap_value = -1;
219 static int hf_snmp_counter_value = -1;
220 static int hf_snmp_timeticks_value = -1;
221 static int hf_snmp_big_counter_value = -1;
222 static int hf_snmp_gauge32_value = -1;
223
224 static int hf_snmp_objectname = -1;
225 static int hf_snmp_scalar_instance_index = -1;
226
227
228
229 /*--- Included file: packet-snmp-hf.c ---*/
230 #line 1 "packet-snmp-hf.c"
231 static int hf_snmp_SMUX_PDUs_PDU = -1;            /* SMUX_PDUs */
232 static int hf_snmp_version = -1;                  /* Version */
233 static int hf_snmp_community = -1;                /* Community */
234 static int hf_snmp_data = -1;                     /* PDUs */
235 static int hf_snmp_parameters = -1;               /* OCTET_STRING */
236 static int hf_snmp_datav2u = -1;                  /* T_datav2u */
237 static int hf_snmp_v2u_plaintext = -1;            /* PDUs */
238 static int hf_snmp_encrypted = -1;                /* OCTET_STRING */
239 static int hf_snmp_msgAuthoritativeEngineID = -1;  /* T_msgAuthoritativeEngineID */
240 static int hf_snmp_msgAuthoritativeEngineBoots = -1;  /* T_msgAuthoritativeEngineBoots */
241 static int hf_snmp_msgAuthoritativeEngineTime = -1;  /* T_msgAuthoritativeEngineTime */
242 static int hf_snmp_msgUserName = -1;              /* T_msgUserName */
243 static int hf_snmp_msgAuthenticationParameters = -1;  /* T_msgAuthenticationParameters */
244 static int hf_snmp_msgPrivacyParameters = -1;     /* T_msgPrivacyParameters */
245 static int hf_snmp_msgVersion = -1;               /* Version */
246 static int hf_snmp_msgGlobalData = -1;            /* HeaderData */
247 static int hf_snmp_msgSecurityParameters = -1;    /* T_msgSecurityParameters */
248 static int hf_snmp_msgData = -1;                  /* ScopedPduData */
249 static int hf_snmp_msgID = -1;                    /* INTEGER_0_2147483647 */
250 static int hf_snmp_msgMaxSize = -1;               /* INTEGER_484_2147483647 */
251 static int hf_snmp_msgFlags = -1;                 /* T_msgFlags */
252 static int hf_snmp_msgSecurityModel = -1;         /* T_msgSecurityModel */
253 static int hf_snmp_plaintext = -1;                /* ScopedPDU */
254 static int hf_snmp_encryptedPDU = -1;             /* T_encryptedPDU */
255 static int hf_snmp_contextEngineID = -1;          /* SnmpEngineID */
256 static int hf_snmp_contextName = -1;              /* OCTET_STRING */
257 static int hf_snmp_get_request = -1;              /* GetRequest_PDU */
258 static int hf_snmp_get_next_request = -1;         /* GetNextRequest_PDU */
259 static int hf_snmp_get_response = -1;             /* GetResponse_PDU */
260 static int hf_snmp_set_request = -1;              /* SetRequest_PDU */
261 static int hf_snmp_trap = -1;                     /* Trap_PDU */
262 static int hf_snmp_getBulkRequest = -1;           /* GetBulkRequest_PDU */
263 static int hf_snmp_informRequest = -1;            /* InformRequest_PDU */
264 static int hf_snmp_sNMPv2_Trap = -1;              /* SNMPv2_Trap_PDU */
265 static int hf_snmp_report = -1;                   /* Report_PDU */
266 static int hf_snmp_request_id = -1;               /* INTEGER */
267 static int hf_snmp_error_status = -1;             /* T_error_status */
268 static int hf_snmp_error_index = -1;              /* INTEGER */
269 static int hf_snmp_variable_bindings = -1;        /* VarBindList */
270 static int hf_snmp_bulkPDU_request_id = -1;       /* Integer32 */
271 static int hf_snmp_non_repeaters = -1;            /* INTEGER_0_2147483647 */
272 static int hf_snmp_max_repetitions = -1;          /* INTEGER_0_2147483647 */
273 static int hf_snmp_enterprise = -1;               /* EnterpriseOID */
274 static int hf_snmp_agent_addr = -1;               /* NetworkAddress */
275 static int hf_snmp_generic_trap = -1;             /* GenericTrap */
276 static int hf_snmp_specific_trap = -1;            /* SpecificTrap */
277 static int hf_snmp_time_stamp = -1;               /* TimeTicks */
278 static int hf_snmp_name = -1;                     /* ObjectName */
279 static int hf_snmp_valueType = -1;                /* NULL */
280 static int hf_snmp_VarBindList_item = -1;         /* VarBind */
281 static int hf_snmp_open = -1;                     /* OpenPDU */
282 static int hf_snmp_close = -1;                    /* ClosePDU */
283 static int hf_snmp_registerRequest = -1;          /* RReqPDU */
284 static int hf_snmp_registerResponse = -1;         /* RegisterResponse */
285 static int hf_snmp_commitOrRollback = -1;         /* SOutPDU */
286 static int hf_snmp_rRspPDU = -1;                  /* RRspPDU */
287 static int hf_snmp_pDUs = -1;                     /* PDUs */
288 static int hf_snmp_smux_simple = -1;              /* SimpleOpen */
289 static int hf_snmp_smux_version = -1;             /* T_smux_version */
290 static int hf_snmp_identity = -1;                 /* OBJECT_IDENTIFIER */
291 static int hf_snmp_description = -1;              /* DisplayString */
292 static int hf_snmp_password = -1;                 /* OCTET_STRING */
293 static int hf_snmp_subtree = -1;                  /* ObjectName */
294 static int hf_snmp_priority = -1;                 /* INTEGER_M1_2147483647 */
295 static int hf_snmp_operation = -1;                /* T_operation */
296
297 /*--- End of included file: packet-snmp-hf.c ---*/
298 #line 221 "packet-snmp-template.c"
299
300 static int hf_smux_version = -1;
301 static int hf_smux_pdutype = -1;
302
303 /* Initialize the subtree pointers */
304 static gint ett_smux = -1;
305 static gint ett_snmp = -1;
306 static gint ett_engineid = -1;
307 static gint ett_msgFlags = -1;
308 static gint ett_encryptedPDU = -1;
309 static gint ett_decrypted = -1;
310 static gint ett_authParameters = -1;
311 static gint ett_internet = -1;
312 static gint ett_varbind = -1;
313 static gint ett_name = -1;
314 static gint ett_value = -1;
315 static gint ett_decoding_error = -1;
316
317
318 /*--- Included file: packet-snmp-ett.c ---*/
319 #line 1 "packet-snmp-ett.c"
320 static gint ett_snmp_Message = -1;
321 static gint ett_snmp_Messagev2u = -1;
322 static gint ett_snmp_T_datav2u = -1;
323 static gint ett_snmp_UsmSecurityParameters = -1;
324 static gint ett_snmp_SNMPv3Message = -1;
325 static gint ett_snmp_HeaderData = -1;
326 static gint ett_snmp_ScopedPduData = -1;
327 static gint ett_snmp_ScopedPDU = -1;
328 static gint ett_snmp_PDUs = -1;
329 static gint ett_snmp_PDU = -1;
330 static gint ett_snmp_BulkPDU = -1;
331 static gint ett_snmp_Trap_PDU_U = -1;
332 static gint ett_snmp_VarBind = -1;
333 static gint ett_snmp_VarBindList = -1;
334 static gint ett_snmp_SMUX_PDUs = -1;
335 static gint ett_snmp_RegisterResponse = -1;
336 static gint ett_snmp_OpenPDU = -1;
337 static gint ett_snmp_SimpleOpen_U = -1;
338 static gint ett_snmp_RReqPDU_U = -1;
339
340 /*--- End of included file: packet-snmp-ett.c ---*/
341 #line 240 "packet-snmp-template.c"
342
343 static const true_false_string auth_flags = {
344         "OK",
345         "Failed"
346 };
347
348 /* Security Models */
349
350 #define SNMP_SEC_ANY                    0
351 #define SNMP_SEC_V1                             1
352 #define SNMP_SEC_V2C                    2
353 #define SNMP_SEC_USM                    3
354
355 static const value_string sec_models[] = {
356         { SNMP_SEC_ANY,                 "Any" },
357         { SNMP_SEC_V1,                  "V1" },
358         { SNMP_SEC_V2C,                 "V2C" },
359         { SNMP_SEC_USM,                 "USM" },
360         { 0,                            NULL }
361 };
362
363 /* SMUX PDU types */
364 #define SMUX_MSG_OPEN           0
365 #define SMUX_MSG_CLOSE          1
366 #define SMUX_MSG_RREQ           2
367 #define SMUX_MSG_RRSP           3
368 #define SMUX_MSG_SOUT           4
369
370 static const value_string smux_types[] = {
371         { SMUX_MSG_OPEN,        "Open" },
372         { SMUX_MSG_CLOSE,       "Close" },
373         { SMUX_MSG_RREQ,        "Registration Request" },
374         { SMUX_MSG_RRSP,        "Registration Response" },
375         { SMUX_MSG_SOUT,        "Commit Or Rollback" },
376         { 0,                    NULL }
377 };
378
379
380 #define SNMP_IPA    0           /* IP Address */
381 #define SNMP_CNT    1           /* Counter (Counter32) */
382 #define SNMP_GGE    2           /* Gauge (Gauge32) */
383 #define SNMP_TIT    3           /* TimeTicks */
384 #define SNMP_OPQ    4           /* Opaque */
385 #define SNMP_NSP    5           /* NsapAddress */
386 #define SNMP_C64    6           /* Counter64 */
387 #define SNMP_U32    7           /* Uinteger32 */
388
389 #define SERR_NSO    0
390 #define SERR_NSI    1
391 #define SERR_EOM    2
392
393
394 dissector_table_t value_sub_dissectors_table;
395
396
397 static const gchar *
398 snmp_lookup_specific_trap (guint specific_trap)
399 {
400         guint i;
401
402         for (i = 0; i < num_specific_traps; i++) {
403                 snmp_st_assoc_t *u = &(specific_traps[i]);
404
405                 if ((u->trap == specific_trap) &&
406                     (strcmp (u->enterprise, enterprise_oid) == 0))
407                 {
408                         return u->desc;
409                 }
410         }
411
412         return NULL;
413 }
414
415 /*
416  *  dissect_snmp_VarBind
417  *  this routine dissects variable bindings, looking for the oid information in our oid reporsitory
418  *  to format and add the value adequatelly.
419  *
420  * The choice to handwrite this code instead of using the asn compiler is to avoid having tons
421  * of uses of global variables distributed in very different parts of the code.
422  * Other than that there's a cosmetic thing: the tree from ASN generated code would be so
423  * convoluted due to the nesting of CHOICEs in the definition of VarBind/value.
424  *
425  * XXX: the length of this function (~400 lines) is an aberration!
426  *  oid_key_t:key_type could become a series of callbacks instead of an enum
427  *  the (! oid_info_is_ok) switch could be made into an array (would be slower)
428  *
429
430         NetworkAddress ::=  CHOICE { internet IpAddress }
431         IpAddress ::= [APPLICATION 0] IMPLICIT OCTET STRING (SIZE (4))
432         TimeTicks ::= [APPLICATION 3] IMPLICIT INTEGER (0..4294967295)
433         Integer32 ::= INTEGER (-2147483648..2147483647)
434         ObjectName ::= OBJECT IDENTIFIER
435         Counter32 ::= [APPLICATION 1] IMPLICIT INTEGER (0..4294967295)
436         Gauge32 ::= [APPLICATION 2] IMPLICIT INTEGER (0..4294967295)
437         Unsigned32 ::= [APPLICATION 2] IMPLICIT INTEGER (0..4294967295)
438         Integer-value ::=  INTEGER (-2147483648..2147483647)
439         Integer32 ::= INTEGER (-2147483648..2147483647)
440         ObjectID-value ::= OBJECT IDENTIFIER
441         Empty ::= NULL
442         TimeTicks ::= [APPLICATION 3] IMPLICIT INTEGER (0..4294967295)
443         Opaque ::= [APPLICATION 4] IMPLICIT OCTET STRING
444         Counter64 ::= [APPLICATION 6] IMPLICIT INTEGER (0..18446744073709551615)
445
446         ObjectSyntax ::= CHOICE {
447                  simple SimpleSyntax,
448                  application-wide ApplicationSyntax
449         }
450
451         SimpleSyntax ::= CHOICE {
452            integer-value Integer-value,
453            string-value String-value,
454            objectID-value ObjectID-value,
455            empty  Empty
456         }
457
458         ApplicationSyntax ::= CHOICE {
459            ipAddress-value IpAddress,
460            counter-value Counter32,
461            timeticks-value TimeTicks,
462            arbitrary-value Opaque,
463            big-counter-value Counter64,
464            unsigned-integer-value Unsigned32
465         }
466
467         ValueType ::=  CHOICE {
468            value ObjectSyntax,
469            unSpecified NULL,
470            noSuchObject[0] IMPLICIT NULL,
471            noSuchInstance[1] IMPLICIT NULL,
472            endOfMibView[2] IMPLICIT NULL
473         }
474
475         VarBind ::= SEQUENCE {
476            name ObjectName,
477            valueType ValueType
478         }
479
480  */
481
482 extern int
483 dissect_snmp_VarBind(gboolean implicit_tag _U_, tvbuff_t *tvb, int offset,
484                      asn1_ctx_t *actx, proto_tree *tree, int hf_index _U_)
485 {
486         int seq_offset, name_offset, value_offset, value_start;
487         guint32 seq_len, name_len, value_len;
488         gint8 ber_class;
489         gboolean pc;
490         gint32 tag;
491         gboolean ind;
492         guint32* subids;
493         guint8* oid_bytes;
494         oid_info_t* oid_info = NULL;
495         guint oid_matched, oid_left;
496         proto_item *pi_name, *pi_varbind, *pi_value = NULL;
497         proto_tree *pt, *pt_varbind, *pt_name, *pt_value;
498         char label[ITEM_LABEL_LENGTH];
499         const char* repr = NULL;
500         const char* info_oid = NULL;
501         char* valstr;
502         int hfid = -1;
503         int min_len = 0, max_len = 0;
504         gboolean oid_info_is_ok;
505         const char* oid_string = NULL;
506         enum {BER_NO_ERROR, BER_WRONG_LENGTH, BER_WRONG_TAG} format_error = BER_NO_ERROR;
507
508         seq_offset = offset;
509
510         /* first have the VarBind's sequence header */
511         offset = get_ber_identifier(tvb, offset, &ber_class, &pc, &tag);
512         offset = get_ber_length(tvb, offset, &seq_len, &ind);
513
514         seq_len += offset - seq_offset;
515
516         if (!pc && ber_class==BER_CLASS_UNI && tag==BER_UNI_TAG_SEQUENCE) {
517                 proto_item* pi = proto_tree_add_text(tree, tvb, seq_offset, seq_len,"VarBind must be an universal class sequence");
518                 pt = proto_item_add_subtree(pi,ett_decoding_error);
519                 expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "VarBind is not an universal class sequence");
520                 return dissect_unknown_ber(actx->pinfo, tvb, seq_offset, pt);
521         }
522
523         if (ind) {
524                 proto_item* pi = proto_tree_add_text(tree, tvb, seq_offset, seq_len,"Indicator must be clear in VarBind");
525                 pt = proto_item_add_subtree(pi,ett_decoding_error);
526                 expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "VarBind has indicator set");
527                 return dissect_unknown_ber(actx->pinfo, tvb, seq_offset, pt);
528         }
529
530         /* then we have the ObjectName's header */
531
532         offset = get_ber_identifier(tvb, offset, &ber_class, &pc, &tag);
533         name_offset = offset = get_ber_length(tvb, offset, &name_len, &ind);
534
535         if (! ( !pc && ber_class==BER_CLASS_UNI && tag==BER_UNI_TAG_OID) ) {
536                 proto_item* pi = proto_tree_add_text(tree, tvb, seq_offset, seq_len,"ObjectName must be an OID in primitive encoding");
537                 pt = proto_item_add_subtree(pi,ett_decoding_error);
538                 expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "ObjectName not an OID");
539                 return dissect_unknown_ber(actx->pinfo, tvb, seq_offset, pt);
540         }
541
542         if (ind) {
543                 proto_item* pi = proto_tree_add_text(tree, tvb, seq_offset, seq_len,"Indicator must be clear in ObjectName");
544                 pt = proto_item_add_subtree(pi,ett_decoding_error);
545                 expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "ObjectName has indicator set");
546                 return dissect_unknown_ber(actx->pinfo, tvb, seq_offset, pt);
547         }
548
549         offset += name_len;
550         value_start = offset;
551
552         /* then we have the  value's header */
553         offset = get_ber_identifier(tvb, offset, &ber_class, &pc, &tag);
554         value_offset = offset = get_ber_length(tvb, offset, &value_len, &ind);
555
556         if (! (!pc) ) {
557                 proto_item* pi = proto_tree_add_text(tree, tvb, seq_offset, seq_len,"the value must be in primitive encoding");
558                 pt = proto_item_add_subtree(pi,ett_decoding_error);
559                 expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "value not in primitive encoding");
560                 return dissect_unknown_ber(actx->pinfo, tvb, seq_offset, pt);
561         }
562
563         /* Now, we know where everithing is */
564
565
566
567         /* we add the varbind tree root with a dummy label we'll fill later on */
568         pi_varbind = proto_tree_add_text(tree,tvb,seq_offset,seq_len,"VarBind");
569         pt_varbind = proto_item_add_subtree(pi_varbind,ett_varbind);
570         *label = '\0';
571
572         pi_name = proto_tree_add_item(pt_varbind,hf_snmp_objectname,tvb,name_offset,name_len,FALSE);
573         pt_name = proto_item_add_subtree(pi_name,ett_name);
574
575         /* fetch ObjectName and its relative oid_info */
576         oid_bytes = ep_tvb_memdup(tvb, name_offset, name_len);
577         oid_info = oid_get_from_encoded(oid_bytes, name_len, &subids, &oid_matched, &oid_left);
578
579         add_oid_debug_subtree(oid_info,pt_name);
580
581         if (!subids) {
582                 proto_item* pi;
583
584                 repr = oid_encoded2string(oid_bytes, name_len);
585                 pi = proto_tree_add_text(pt_name,tvb, 0, 0, "invalid oid: %s", repr);
586                 pt = proto_item_add_subtree(pi, ett_decoding_error);
587                 expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "invalid oid: %s", repr);
588                 return dissect_unknown_ber(actx->pinfo, tvb, name_offset, pt);
589         }
590
591         if (oid_matched+oid_left) {
592                 oid_string = oid_subid2string(subids,oid_matched+oid_left);
593         }
594
595         if (ber_class == BER_CLASS_CON) {
596                 /* if we have an error value just add it and get out the way ASAP */
597                 proto_item* pi;
598                 const char* note;
599
600                 if (value_len != 0) {
601                         min_len = max_len = 0;
602                         format_error = BER_WRONG_LENGTH;
603                 }
604
605                 switch (tag) {
606                         case SERR_NSO:
607                                 hfid = hf_snmp_noSuchObject;
608                                 note = "noSuchObject";
609                                 break;
610                         case SERR_NSI:
611                                 hfid = hf_snmp_noSuchInstance;
612                                 note = "noSuchInstance";
613                                 break;
614                         case SERR_EOM:
615                                 hfid = hf_snmp_endOfMibView;
616                                 note = "endOfMibView";
617                                 break;
618                         default: {
619                                 pi = proto_tree_add_text(pt_varbind,tvb,0,0,"Wrong tag for Error Value: expected 0, 1, or 2 but got: %d",tag);
620                                 pt = proto_item_add_subtree(pi,ett_decoding_error);
621                                 expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "Wrong tag for SNMP VarBind error value");
622                                 return dissect_unknown_ber(actx->pinfo, tvb, value_start, pt);
623                         }
624                 }
625
626                 pi = proto_tree_add_item(pt_varbind,hfid,tvb,value_offset,value_len,FALSE);
627                 expert_add_info_format(actx->pinfo, pi, PI_RESPONSE_CODE, PI_NOTE, "%s",note);
628                 g_strlcpy (label, note, ITEM_LABEL_LENGTH);
629                 goto set_label;
630         }
631
632         /* now we'll try to figure out which are the indexing sub-oids and whether the oid we know about is the one oid we have to use */
633         switch (oid_info->kind) {
634                 case OID_KIND_SCALAR:
635                         if (oid_left  == 1) {
636                                 /* OK: we got the instance sub-id */
637                                 proto_tree_add_uint64(pt_name,hf_snmp_scalar_instance_index,tvb,name_offset,name_len,subids[oid_matched]);
638                                 oid_info_is_ok = TRUE;
639                                 goto indexing_done;
640                         } else if (oid_left  == 0) {
641                                 if (ber_class == BER_CLASS_UNI && tag == BER_UNI_TAG_NULL) {
642                                         /* unSpecified  does not require an instance sub-id add the new value and get off the way! */
643                                         pi_value = proto_tree_add_item(pt_varbind,hf_snmp_unSpecified,tvb,value_offset,value_len,FALSE);
644                                         goto set_label;
645                                 } else {
646                                         proto_item* pi = proto_tree_add_text(pt_name,tvb,0,0,"A scalar should have one instance sub-id this one has none");
647                                         expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "No instance sub-id in scalar value");
648                                         oid_info_is_ok = FALSE;
649                                         goto indexing_done;
650                                 }
651                         } else {
652                                 proto_item* pi = proto_tree_add_text(pt_name,tvb,0,0,"A scalar should have only one instance sub-id this has: %d",oid_left);
653                                 expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "Wrong number of instance sub-ids in scalar value");
654                                 oid_info_is_ok = FALSE;
655                                 goto indexing_done;
656                         }
657                 break;
658                 case OID_KIND_COLUMN:
659                         if ( oid_info->parent->kind == OID_KIND_ROW) {
660                                 oid_key_t* k = oid_info->parent->key;
661                                 guint key_start = oid_matched;
662                                 guint key_len = oid_left;
663                                 oid_info_is_ok = TRUE;
664
665                                 if ( key_len == 0 && ber_class == BER_CLASS_UNI && tag == BER_UNI_TAG_NULL) {
666                                         /* unSpecified  does not require an instance sub-id add the new value and get off the way! */
667                                         pi_value = proto_tree_add_item(pt_varbind,hf_snmp_unSpecified,tvb,value_offset,value_len,FALSE);
668                                         goto set_label;
669                                 }
670
671                                 if (k) {
672                                         for (;k;k = k->next) {
673                                                 guint suboid_len;
674
675                                                 if (key_start >= oid_matched+oid_left) {
676                                                         proto_item* pi = proto_tree_add_text(pt_name,tvb,0,0,"index sub-oid shorter than expected");
677                                                         expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "index sub-oid shorter than expected");
678                                                         oid_info_is_ok = FALSE;
679                                                         goto indexing_done;
680                                                 }
681
682                                                 switch(k->key_type) {
683                                                         case OID_KEY_TYPE_WRONG: {
684                                                                 proto_item* pi = proto_tree_add_text(pt_name,tvb,0,0,"OID instaces not handled, if you want this implemented please contact the wireshark developers");
685                                                                 expert_add_info_format(actx->pinfo, pi, PI_UNDECODED, PI_WARN, "Unimplemented instance index");
686                                                                 oid_info_is_ok = FALSE;
687                                                                 goto indexing_done;
688                                                         }
689                                                         case OID_KEY_TYPE_INTEGER: {
690                                                                 if (IS_FT_INT(k->ft_type)) {
691                                                                         proto_tree_add_int(pt_name,k->hfid,tvb,name_offset,name_len,(guint)subids[key_start]);
692                                                                 } else { /* if it's not an unsigned int let proto_tree_add_uint throw a warning */
693                                                                         proto_tree_add_uint(pt_name,k->hfid,tvb,name_offset,name_len,(guint)subids[key_start]);
694                                                                 }
695                                                                 key_start++;
696                                                                 key_len--;
697                                                                 continue; /* k->next */
698                                                         }
699                                                         case OID_KEY_TYPE_IMPLIED_OID:
700                                                                 suboid_len = key_len;
701
702                                                                 goto show_oid_index;
703
704                                                         case OID_KEY_TYPE_OID: {
705                                                                 guint8* suboid_buf;
706                                                                 guint suboid_buf_len;
707                                                                 guint32* suboid;
708
709                                                                 suboid_len = subids[key_start++];
710                                                                 key_len--;
711
712 show_oid_index:
713                                                                 suboid = &(subids[key_start]);
714
715                                                                 if( suboid_len == 0 ) {
716                                                                         proto_item* pi = proto_tree_add_text(pt_name,tvb,0,0,"an index sub-oid OID cannot be 0 bytes long!");
717                                                                         expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "index sub-oid OID with len=0");
718                                                                         oid_info_is_ok = FALSE;
719                                                                         goto indexing_done;
720                                                                 }
721
722                                                                 if( key_len < suboid_len ) {
723                                                                         proto_item* pi = proto_tree_add_text(pt_name,tvb,0,0,"index sub-oid should not be longer than remaining oid size");
724                                                                         expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "index sub-oid longer than remaining oid size");
725                                                                         oid_info_is_ok = FALSE;
726                                                                         goto indexing_done;
727                                                                 }
728
729                                                                 suboid_buf_len = oid_subid2encoded(suboid_len, suboid, &suboid_buf);
730
731                                                                 DISSECTOR_ASSERT(suboid_buf_len);
732
733                                                                 proto_tree_add_oid(pt_name,k->hfid,tvb,name_offset, suboid_buf_len, suboid_buf);
734
735                                                                 key_start += suboid_len;
736                                                                 key_len -= suboid_len + 1;
737                                                                 continue; /* k->next */
738                                                         }
739                                                         default: {
740                                                                 guint8* buf;
741                                                                 guint buf_len;
742                                                                 guint32* suboid;
743                                                                 guint i;
744
745
746                                                                 switch (k->key_type) {
747                                                                         case OID_KEY_TYPE_IPADDR:
748                                                                                 suboid = &(subids[key_start]);
749                                                                                 buf_len = 4;
750                                                                                 break;
751                                                                         case OID_KEY_TYPE_IMPLIED_STRING:
752                                                                         case OID_KEY_TYPE_IMPLIED_BYTES:
753                                                                         case OID_KEY_TYPE_ETHER:
754                                                                                 suboid = &(subids[key_start]);
755                                                                                 buf_len = key_len;
756                                                                                 break;
757                                                                         default:
758                                                                                 buf_len = k->num_subids;
759                                                                                 suboid = &(subids[key_start]);
760
761                                                                                 if(!buf_len) {
762                                                                                         buf_len = *suboid++;
763                                                                                         key_len--;
764                                                                                         key_start++;
765                                                                                 }
766                                                                                 break;
767                                                                 }
768
769                                                                 if( key_len < buf_len ) {
770                                                                         proto_item* pi = proto_tree_add_text(pt_name,tvb,0,0,"index string should not be longer than remaining oid size");
771                                                                         expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "index string longer than remaining oid size");
772                                                                         oid_info_is_ok = FALSE;
773                                                                         goto indexing_done;
774                                                                 }
775
776                                                                 buf = ep_alloc(buf_len+1);
777                                                                 for (i = 0; i < buf_len; i++)
778                                                                         buf[i] = (guint8)suboid[i];
779                                                                 buf[i] = '\0';
780
781                                                                 switch(k->key_type) {
782                                                                         case OID_KEY_TYPE_STRING:
783                                                                         case OID_KEY_TYPE_IMPLIED_STRING:
784                                                                                 proto_tree_add_string(pt_name,k->hfid,tvb,name_offset,buf_len, buf);
785                                                                                 break;
786                                                                         case OID_KEY_TYPE_BYTES:
787                                                                         case OID_KEY_TYPE_NSAP:
788                                                                         case OID_KEY_TYPE_IMPLIED_BYTES:
789                                                                                 proto_tree_add_bytes(pt_name,k->hfid,tvb,name_offset,buf_len, buf);
790                                                                                 break;
791                                                                         case OID_KEY_TYPE_ETHER:
792                                                                                 proto_tree_add_ether(pt_name,k->hfid,tvb,name_offset,buf_len, buf);
793                                                                                 break;
794                                                                         case OID_KEY_TYPE_IPADDR: {
795                                                                                 guint32* ipv4_p = (void*)buf;
796                                                                                 proto_tree_add_ipv4(pt_name,k->hfid,tvb,name_offset,buf_len, *ipv4_p);
797                                                                                 break;
798                                                                         default:
799                                                                                 DISSECTOR_ASSERT_NOT_REACHED();
800                                                                                 break;
801                                                                         }
802                                                                 }
803
804                                                                 key_start += buf_len;
805                                                                 key_len -= buf_len;
806                                                                 continue; /* k->next*/
807                                                         }
808                                                 }
809                                         }
810                                         goto indexing_done;
811                                 } else {
812                                         proto_item* pi = proto_tree_add_text(pt_name,tvb,0,0,"We do not know how to handle this OID, if you want this implemented please contact the wireshark developers");
813                                         expert_add_info_format(actx->pinfo, pi, PI_UNDECODED, PI_WARN, "Unimplemented instance index");
814                                         oid_info_is_ok = FALSE;
815                                         goto indexing_done;
816                                 }
817                         } else {
818                                 proto_item* pi = proto_tree_add_text(pt_name,tvb,0,0,"The COLUMS's parent is not a ROW. This is a BUG! please contact the wireshark developers.");
819                                 expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_ERROR, "COLUMS's parent is not a ROW");
820                                 oid_info_is_ok = FALSE;
821                                 goto indexing_done;
822                         }
823                 default: {
824 /*                      proto_item* pi = proto_tree_add_text(pt_name,tvb,0,0,"This kind OID should have no value");
825                         expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "This kind OID should have no value"); */
826                         oid_info_is_ok = FALSE;
827                         goto indexing_done;
828                 }
829         }
830 indexing_done:
831
832         if (oid_info_is_ok && oid_info->value_type) {
833                 if (ber_class == BER_CLASS_UNI && tag == BER_UNI_TAG_NULL) {
834                         pi_value = proto_tree_add_item(pt_varbind,hf_snmp_unSpecified,tvb,value_offset,value_len,FALSE);
835                 }  else {
836                         if ((oid_info->value_type->ber_class != BER_CLASS_ANY) &&
837                                 (ber_class != oid_info->value_type->ber_class))
838                                 format_error = BER_WRONG_TAG;
839
840                         if ((oid_info->value_type->ber_tag != BER_TAG_ANY) &&
841                                 (tag != oid_info->value_type->ber_tag))
842                                 format_error = BER_WRONG_TAG;
843
844                         max_len = oid_info->value_type->max_len == -1 ? 0xffffff : oid_info->value_type->max_len;
845                         min_len  = oid_info->value_type->min_len;
846
847                         if ((int)value_len < min_len || (int)value_len > max_len) {
848                                 format_error = BER_WRONG_LENGTH;
849                         } else {
850                                 pi_value = proto_tree_add_item(pt_varbind,oid_info->value_hfid,tvb,value_offset,value_len,FALSE);
851                         }
852                 }
853         } else {
854                 switch(ber_class|(tag<<4)) {
855                         case BER_CLASS_UNI|(BER_UNI_TAG_INTEGER<<4):
856                         {
857                                 gint64 val=0;
858                                 unsigned offset = value_offset;
859                                 unsigned i;
860
861                                 max_len = 5; min_len = 1;
862                                 if (value_len > (guint)max_len && value_len < (guint)min_len) {
863                                         format_error = BER_WRONG_LENGTH;
864                                         break;
865                                 }
866
867                                 if(value_len > 0) {
868                                         /* extend sign bit */
869                                         if(tvb_get_guint8(tvb, offset)&0x80) {
870                                                 val=-1;
871                                         }
872                                         for(i=0;i<value_len;i++) {
873                                                 val=(val<<8)|tvb_get_guint8(tvb, offset);
874                                                 offset++;
875                                         }
876                                 }
877                                 proto_tree_add_int64(pt_varbind, hf_snmp_integer32_value, tvb,value_offset,value_len, val);
878
879                                 goto already_added;
880                         }
881                         case BER_CLASS_UNI|(BER_UNI_TAG_OCTETSTRING<<4):
882                                 hfid = hf_snmp_octetstring_value;
883                                 break;
884                         case BER_CLASS_UNI|(BER_UNI_TAG_OID<<4):
885                                 max_len = -1; min_len = 1;
886                                 if (value_len < (guint)min_len) format_error = BER_WRONG_LENGTH;
887                                 hfid = hf_snmp_oid_value;
888                                 break;
889                         case BER_CLASS_UNI|(BER_UNI_TAG_NULL<<4):
890                                 max_len = 0; min_len = 0;
891                                 if (value_len != 0) format_error = BER_WRONG_LENGTH;
892                                 hfid = hf_snmp_null_value;
893                                 break;
894                         case BER_CLASS_APP: /* | (SNMP_IPA<<4)*/
895                                 switch(value_len) {
896                                         case 4: hfid = hf_snmp_ipv4_value; break;
897                                         case 16: hfid = hf_snmp_ipv6_value; break;
898                                         default: hfid = hf_snmp_anyaddress_value; break;
899                                 }
900                                 break;
901                         case BER_CLASS_APP|(SNMP_U32<<4):
902                                 hfid = hf_snmp_unsigned32_value;
903                                 break;
904                         case BER_CLASS_APP|(SNMP_GGE<<4):
905                                 hfid = hf_snmp_gauge32_value;
906                                 break;
907                         case BER_CLASS_APP|(SNMP_CNT<<4):
908                                 hfid = hf_snmp_counter_value;
909                                 break;
910                         case BER_CLASS_APP|(SNMP_TIT<<4):
911                                 hfid = hf_snmp_timeticks_value;
912                                 break;
913                         case BER_CLASS_APP|(SNMP_OPQ<<4):
914                                 hfid = hf_snmp_opaque_value;
915                                 break;
916                         case BER_CLASS_APP|(SNMP_NSP<<4):
917                                 hfid = hf_snmp_nsap_value;
918                                 break;
919                         case BER_CLASS_APP|(SNMP_C64<<4):
920                                 hfid = hf_snmp_big_counter_value;
921                                 break;
922                         default:
923                                 hfid = hf_snmp_unknown_value;
924                                 break;
925                 }
926
927                 pi_value = proto_tree_add_item(pt_varbind,hfid,tvb,value_offset,value_len,FALSE);
928                 if (format_error != BER_NO_ERROR) {
929                         expert_add_info_format(actx->pinfo, pi_value, PI_UNDECODED, PI_NOTE, "Unresolved value, Missing MIB");
930                 }
931
932 already_added:
933                 oid_info_is_ok = FALSE;
934         }
935
936         pt_value = proto_item_add_subtree(pi_value,ett_value);
937
938         if (value_len > 0 && oid_string) {
939                 tvbuff_t* sub_tvb = tvb_new_subset(tvb, value_offset, value_len, value_len);
940
941                 next_tvb_add_string(&var_list, sub_tvb, (snmp_var_in_tree) ? pt_value : NULL, value_sub_dissectors_table, oid_string);
942         }
943
944
945 set_label:
946         if (pi_value) proto_item_fill_label(PITEM_FINFO(pi_value), label);
947
948         if (oid_info && oid_info->name) {
949                 if (oid_left >= 1) {
950                         repr  = ep_strdup_printf("%s.%s (%s)", oid_info->name,
951                                                  oid_subid2string(&(subids[oid_matched]),oid_left),
952                                                  oid_subid2string(subids,oid_matched+oid_left));
953                         info_oid = ep_strdup_printf("%s.%s", oid_info->name,
954                                                     oid_subid2string(&(subids[oid_matched]),oid_left));
955                 } else {
956                         repr  = ep_strdup_printf("%s (%s)", oid_info->name,
957                                                  oid_subid2string(subids,oid_matched));
958                         info_oid = oid_info->name;
959                 }
960         } else if (oid_string) {
961                 repr  = ep_strdup(oid_string);
962                 info_oid = oid_string;
963         } else {
964                 repr  = ep_strdup("[Bad OID]");
965         }
966
967         valstr = strstr(label,": ");
968         valstr = valstr ? valstr+2 : label;
969
970         proto_item_set_text(pi_varbind,"%s: %s",repr,valstr);
971
972         if (display_oid && info_oid) {
973           col_append_fstr (actx->pinfo->cinfo, COL_INFO, " %s", info_oid);
974         }
975
976         switch (format_error) {
977                 case BER_WRONG_LENGTH: {
978                         proto_tree* pt = proto_item_add_subtree(pi_value,ett_decoding_error);
979                         proto_item* pi = proto_tree_add_text(pt,tvb,0,0,"Wrong value length: %u  expecting: %u <= len <= %u",
980                                                              value_len, min_len, max_len == -1 ? 0xFFFFFF : max_len);
981                         pt = proto_item_add_subtree(pi,ett_decoding_error);
982                         expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "Wrong length for SNMP VarBind/value");
983                         return dissect_unknown_ber(actx->pinfo, tvb, value_start, pt);
984                 }
985                 case BER_WRONG_TAG: {
986                         proto_tree* pt = proto_item_add_subtree(pi_value,ett_decoding_error);
987                         proto_item* pi = proto_tree_add_text(pt,tvb,0,0,"Wrong class/tag for Value expected: %d,%d got: %d,%d",
988                                                              oid_info->value_type->ber_class, oid_info->value_type->ber_tag,
989                                                              ber_class, tag);
990                         pt = proto_item_add_subtree(pi,ett_decoding_error);
991                         expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "Wrong class/tag for SNMP VarBind/value");
992                         return dissect_unknown_ber(actx->pinfo, tvb, value_start, pt);
993                 }
994                 default:
995                         break;
996         }
997
998         return seq_offset + seq_len;
999 }
1000
1001
1002 #define F_SNMP_ENGINEID_CONFORM 0x80
1003 #define SNMP_ENGINEID_RFC1910 0x00
1004 #define SNMP_ENGINEID_RFC3411 0x01
1005
1006 static const true_false_string tfs_snmp_engineid_conform = {
1007   "RFC3411 (SNMPv3)",
1008   "RFC1910 (Non-SNMPv3)"
1009 };
1010
1011 #define SNMP_ENGINEID_FORMAT_IPV4 0x01
1012 #define SNMP_ENGINEID_FORMAT_IPV6 0x02
1013 #define SNMP_ENGINEID_FORMAT_MACADDRESS 0x03
1014 #define SNMP_ENGINEID_FORMAT_TEXT 0x04
1015 #define SNMP_ENGINEID_FORMAT_OCTETS 0x05
1016
1017 static const value_string snmp_engineid_format_vals[] = {
1018         { SNMP_ENGINEID_FORMAT_IPV4,    "IPv4 address" },
1019         { SNMP_ENGINEID_FORMAT_IPV6,    "IPv6 address" },
1020         { SNMP_ENGINEID_FORMAT_MACADDRESS,      "MAC address" },
1021         { SNMP_ENGINEID_FORMAT_TEXT,    "Text, administratively assigned" },
1022         { SNMP_ENGINEID_FORMAT_OCTETS,  "Octets, administratively assigned" },
1023         { 0,    NULL }
1024 };
1025
1026 #define SNMP_ENGINEID_CISCO_AGENT 0x00
1027 #define SNMP_ENGINEID_CISCO_MANAGER 0x01
1028
1029 static const value_string snmp_engineid_cisco_type_vals[] = {
1030         { SNMP_ENGINEID_CISCO_AGENT,    "Agent" },
1031         { SNMP_ENGINEID_CISCO_MANAGER,  "Manager" },
1032         { 0,    NULL }
1033 };
1034
1035 /*
1036  * SNMP Engine ID dissection according to RFC 3411 (SnmpEngineID TC)
1037  * or historic RFC 1910 (AgentID)
1038  */
1039 int
1040 dissect_snmp_engineid(proto_tree *tree, tvbuff_t *tvb, int offset, int len)
1041 {
1042     proto_item *item = NULL;
1043     guint8 conformance, format;
1044     guint32 enterpriseid, seconds;
1045     nstime_t ts;
1046     int len_remain = len;
1047
1048     /* first bit: engine id conformance */
1049     if (len_remain<4) return offset;
1050     conformance = ((tvb_get_guint8(tvb, offset)>>7) & 0x01);
1051     proto_tree_add_item(tree, hf_snmp_engineid_conform, tvb, offset, 1, FALSE);
1052
1053     /* 4-byte enterprise number/name */
1054     if (len_remain<4) return offset;
1055     enterpriseid = tvb_get_ntohl(tvb, offset);
1056     if (conformance)
1057       enterpriseid -= 0x80000000; /* ignore first bit */
1058     proto_tree_add_uint(tree, hf_snmp_engineid_enterprise, tvb, offset, 4, enterpriseid);
1059     offset+=4;
1060     len_remain-=4;
1061
1062     switch(conformance) {
1063
1064     case SNMP_ENGINEID_RFC1910:
1065       /* 12-byte AgentID w/ 8-byte trailer */
1066       if (len_remain==8) {
1067         proto_tree_add_text(tree, tvb, offset, 8, "AgentID Trailer: 0x%s",
1068                             tvb_bytes_to_str(tvb, offset, 8));
1069         offset+=8;
1070         len_remain-=8;
1071       } else {
1072         proto_tree_add_text(tree, tvb, offset, len_remain, "<Data not conforming to RFC1910>");
1073         return offset;
1074       }
1075       break;
1076
1077     case SNMP_ENGINEID_RFC3411: /* variable length: 5..32 */
1078
1079       /* 1-byte format specifier */
1080       if (len_remain<1) return offset;
1081       format = tvb_get_guint8(tvb, offset);
1082       item = proto_tree_add_uint_format(tree, hf_snmp_engineid_format, tvb, offset, 1, format, "Engine ID Format: %s (%d)",
1083                                         val_to_str(format, snmp_engineid_format_vals, "Reserved/Enterprise-specific"), format);
1084       offset+=1;
1085       len_remain-=1;
1086
1087       switch(format) {
1088       case SNMP_ENGINEID_FORMAT_IPV4:
1089         /* 4-byte IPv4 address */
1090         if (len_remain==4) {
1091           proto_tree_add_item(tree, hf_snmp_engineid_ipv4, tvb, offset, 4, FALSE);
1092           offset+=4;
1093           len_remain=0;
1094         }
1095         break;
1096       case SNMP_ENGINEID_FORMAT_IPV6:
1097         /* 16-byte IPv6 address */
1098         if (len_remain==16) {
1099           proto_tree_add_item(tree, hf_snmp_engineid_ipv6, tvb, offset, 16, FALSE);
1100           offset+=16;
1101           len_remain=0;
1102         }
1103         break;
1104       case SNMP_ENGINEID_FORMAT_MACADDRESS:
1105         /* See: https://supportforums.cisco.com/message/3010617#3010617 for details. */
1106         if ((enterpriseid==9)&&(len_remain==7)) {
1107           proto_tree_add_item(tree, hf_snmp_engineid_cisco_type, tvb, offset, 1, FALSE);
1108           offset++;
1109           len_remain--;
1110         }
1111         /* 6-byte MAC address */
1112         if (len_remain==6) {
1113           proto_tree_add_item(tree, hf_snmp_engineid_mac, tvb, offset, 6, FALSE);
1114           offset+=6;
1115           len_remain=0;
1116         }
1117         break;
1118       case SNMP_ENGINEID_FORMAT_TEXT:
1119         /* max. 27-byte string, administratively assigned */
1120         if (len_remain<=27) {
1121           proto_tree_add_item(tree, hf_snmp_engineid_text, tvb, offset, len_remain, FALSE);
1122           offset+=len_remain;
1123           len_remain=0;
1124         }
1125         break;
1126       case 128:
1127         /* most common enterprise-specific format: (ucd|net)-snmp random */
1128         if ((enterpriseid==2021)||(enterpriseid==8072)) {
1129           proto_item_append_text(item, (enterpriseid==2021) ? ": UCD-SNMP Random" : ": Net-SNMP Random");
1130           /* demystify: 4B random, 4B epoch seconds */
1131           if (len_remain==8) {
1132             proto_tree_add_item(tree, hf_snmp_engineid_data, tvb, offset, 4, FALSE);
1133             seconds = tvb_get_letohl(tvb, offset+4);
1134             ts.secs = seconds;
1135             ts.nsecs = 0;
1136             proto_tree_add_time_format_value(tree, hf_snmp_engineid_time, tvb, offset+4, 4,
1137                                              &ts, "%s",
1138                                              abs_time_secs_to_str(seconds, ABSOLUTE_TIME_LOCAL, TRUE));
1139             offset+=8;
1140             len_remain=0;
1141           }
1142         }
1143         break;
1144       case SNMP_ENGINEID_FORMAT_OCTETS:
1145       default:
1146         /* max. 27 bytes, administratively assigned or unknown format */
1147         if (len_remain<=27) {
1148           proto_tree_add_item(tree, hf_snmp_engineid_data, tvb, offset, len_remain, FALSE);
1149           offset+=len_remain;
1150           len_remain=0;
1151         }
1152         break;
1153       }
1154     }
1155
1156     if (len_remain>0) {
1157       proto_tree_add_text(tree, tvb, offset, len_remain, "<Data not conforming to RFC3411>");
1158       offset+=len_remain;
1159     }
1160     return offset;
1161 }
1162
1163
1164 static void set_ue_keys(snmp_ue_assoc_t* n ) {
1165         guint key_size = n->user.authModel->key_size;
1166
1167         n->user.authKey.data = se_alloc(key_size);
1168         n->user.authKey.len = key_size;
1169         n->user.authModel->pass2key(n->user.authPassword.data,
1170                                     n->user.authPassword.len,
1171                                     n->engine.data,
1172                                     n->engine.len,
1173                                     n->user.authKey.data);
1174
1175         n->user.privKey.data = se_alloc(key_size);
1176         n->user.privKey.len = key_size;
1177         n->user.authModel->pass2key(n->user.privPassword.data,
1178                                     n->user.privPassword.len,
1179                                     n->engine.data,
1180                                     n->engine.len,
1181                                     n->user.privKey.data);
1182 }
1183
1184 static snmp_ue_assoc_t*
1185 ue_se_dup(snmp_ue_assoc_t* o)
1186 {
1187         snmp_ue_assoc_t* d = se_memdup(o,sizeof(snmp_ue_assoc_t));
1188
1189         d->user.authModel = o->user.authModel;
1190
1191         d->user.privProtocol = o->user.privProtocol;
1192
1193         d->user.userName.data = se_memdup(o->user.userName.data,o->user.userName.len);
1194         d->user.userName.len = o->user.userName.len;
1195
1196         d->user.authPassword.data = o->user.authPassword.data ? se_memdup(o->user.authPassword.data,o->user.authPassword.len) : NULL;
1197         d->user.authPassword.len = o->user.authPassword.len;
1198
1199         d->user.privPassword.data = o->user.privPassword.data ? se_memdup(o->user.privPassword.data,o->user.privPassword.len) : NULL;
1200         d->user.privPassword.len = o->user.privPassword.len;
1201
1202         d->engine.len = o->engine.len;
1203
1204         if (d->engine.len) {
1205                 d->engine.data = se_memdup(o->engine.data,o->engine.len);
1206                 set_ue_keys(d);
1207         }
1208
1209         return d;
1210
1211 }
1212
1213
1214 #define CACHE_INSERT(c,a) if (c) { snmp_ue_assoc_t* t = c; c = a; c->next = t; } else { c = a; a->next = NULL; }
1215
1216 static void
1217 renew_ue_cache(void)
1218 {
1219         localized_ues = NULL;
1220         unlocalized_ues = NULL;
1221
1222         if (num_ueas) {
1223                 guint i;
1224
1225                 for(i = 0; i < num_ueas; i++) {
1226                         snmp_ue_assoc_t* a = ue_se_dup(&(ueas[i]));
1227
1228                         if (a->engine.len) {
1229                                 CACHE_INSERT(localized_ues,a);
1230
1231                         } else {
1232                                 CACHE_INSERT(unlocalized_ues,a);
1233                         }
1234
1235                 }
1236         }
1237 }
1238
1239
1240 static snmp_ue_assoc_t*
1241 localize_ue( snmp_ue_assoc_t* o, const guint8* engine, guint engine_len )
1242 {
1243         snmp_ue_assoc_t* n = se_memdup(o,sizeof(snmp_ue_assoc_t));
1244
1245         n->engine.data = se_memdup(engine,engine_len);
1246         n->engine.len = engine_len;
1247
1248         set_ue_keys(n);
1249
1250         return n;
1251 }
1252
1253
1254 #define localized_match(a,u,ul,e,el) \
1255         ( a->user.userName.len == ul \
1256         && a->engine.len == el \
1257         && memcmp( a->user.userName.data, u, ul ) == 0 \
1258         && memcmp( a->engine.data,   e,  el ) == 0 )
1259
1260 #define unlocalized_match(a,u,l) \
1261         ( a->user.userName.len == l && memcmp( a->user.userName.data, u, l) == 0 )
1262
1263 static snmp_ue_assoc_t*
1264 get_user_assoc(tvbuff_t* engine_tvb, tvbuff_t* user_tvb)
1265 {
1266         static snmp_ue_assoc_t* a;
1267         guint given_username_len;
1268         guint8* given_username;
1269         guint given_engine_len;
1270         guint8* given_engine;
1271
1272         if ( ! (localized_ues || unlocalized_ues ) ) return NULL;
1273
1274         if (! ( user_tvb && engine_tvb ) ) return NULL;
1275
1276         given_username_len = tvb_length_remaining(user_tvb,0);
1277         given_username = ep_tvb_memdup(user_tvb,0,-1);
1278         given_engine_len = tvb_length_remaining(engine_tvb,0);
1279         given_engine = ep_tvb_memdup(engine_tvb,0,-1);
1280
1281         for (a = localized_ues; a; a = a->next) {
1282                 if ( localized_match(a, given_username, given_username_len, given_engine, given_engine_len) ) {
1283                         return a;
1284                 }
1285         }
1286
1287         for (a = unlocalized_ues; a; a = a->next) {
1288                 if ( unlocalized_match(a, given_username, given_username_len) ) {
1289                         snmp_ue_assoc_t* n = localize_ue( a, given_engine, given_engine_len );
1290                         CACHE_INSERT(localized_ues,n);
1291                         return n;
1292                 }
1293         }
1294
1295         return NULL;
1296 }
1297
1298 static gboolean
1299 snmp_usm_auth_md5(snmp_usm_params_t* p, guint8** calc_auth_p, guint* calc_auth_len_p, gchar const** error)
1300 {
1301         guint msg_len;
1302         guint8* msg;
1303         guint auth_len;
1304         guint8* auth;
1305         guint8* key;
1306         guint key_len;
1307         guint8 *calc_auth;
1308         guint start;
1309         guint end;
1310         guint i;
1311
1312         if (!p->auth_tvb) {
1313                 *error = "No Authenticator";
1314                 return FALSE;
1315         }
1316
1317         key = p->user_assoc->user.authKey.data;
1318         key_len = p->user_assoc->user.authKey.len;
1319
1320         if (! key ) {
1321                 *error = "User has no authKey";
1322                 return FALSE;
1323         }
1324
1325
1326         auth_len = tvb_length_remaining(p->auth_tvb,0);
1327
1328         if (auth_len != 12) {
1329                 *error = "Authenticator length wrong";
1330                 return FALSE;
1331         }
1332
1333         msg_len = tvb_length_remaining(p->msg_tvb,0);
1334         msg = ep_tvb_memdup(p->msg_tvb,0,msg_len);
1335
1336
1337         auth = ep_tvb_memdup(p->auth_tvb,0,auth_len);
1338
1339         start = p->auth_offset - p->start_offset;
1340         end =   start + auth_len;
1341
1342         /* fill the authenticator with zeros */
1343         for ( i = start ; i < end ; i++ ) {
1344                 msg[i] = '\0';
1345         }
1346
1347         calc_auth = ep_alloc(16);
1348
1349         md5_hmac(msg, msg_len, key, key_len, calc_auth);
1350
1351         if (calc_auth_p) *calc_auth_p = calc_auth;
1352         if (calc_auth_len_p) *calc_auth_len_p = 12;
1353
1354         return ( memcmp(auth,calc_auth,12) != 0 ) ? FALSE : TRUE;
1355 }
1356
1357
1358 static gboolean
1359 snmp_usm_auth_sha1(snmp_usm_params_t* p _U_, guint8** calc_auth_p, guint* calc_auth_len_p,  gchar const** error _U_)
1360 {
1361         guint msg_len;
1362         guint8* msg;
1363         guint auth_len;
1364         guint8* auth;
1365         guint8* key;
1366         guint key_len;
1367         guint8 *calc_auth;
1368         guint start;
1369         guint end;
1370         guint i;
1371
1372         if (!p->auth_tvb) {
1373                 *error = "No Authenticator";
1374                 return FALSE;
1375         }
1376
1377         key = p->user_assoc->user.authKey.data;
1378         key_len = p->user_assoc->user.authKey.len;
1379
1380         if (! key ) {
1381                 *error = "User has no authKey";
1382                 return FALSE;
1383         }
1384
1385
1386         auth_len = tvb_length_remaining(p->auth_tvb,0);
1387
1388
1389         if (auth_len != 12) {
1390                 *error = "Authenticator length wrong";
1391                 return FALSE;
1392         }
1393
1394         msg_len = tvb_length_remaining(p->msg_tvb,0);
1395         msg = ep_tvb_memdup(p->msg_tvb,0,msg_len);
1396
1397         auth = ep_tvb_memdup(p->auth_tvb,0,auth_len);
1398
1399         start = p->auth_offset - p->start_offset;
1400         end =   start + auth_len;
1401
1402         /* fill the authenticator with zeros */
1403         for ( i = start ; i < end ; i++ ) {
1404                 msg[i] = '\0';
1405         }
1406
1407         calc_auth = ep_alloc(20);
1408
1409         sha1_hmac(key, key_len, msg, msg_len, calc_auth);
1410
1411         if (calc_auth_p) *calc_auth_p = calc_auth;
1412         if (calc_auth_len_p) *calc_auth_len_p = 12;
1413
1414         return ( memcmp(auth,calc_auth,12) != 0 ) ? FALSE : TRUE;
1415 }
1416
1417 static tvbuff_t*
1418 snmp_usm_priv_des(snmp_usm_params_t* p _U_, tvbuff_t* encryptedData _U_, gchar const** error _U_)
1419 {
1420 #ifdef HAVE_LIBGCRYPT
1421     gcry_error_t err;
1422     gcry_cipher_hd_t hd = NULL;
1423
1424         guint8* cleartext;
1425         guint8* des_key = p->user_assoc->user.privKey.data; /* first 8 bytes */
1426         guint8* pre_iv = &(p->user_assoc->user.privKey.data[8]); /* last 8 bytes */
1427         guint8* salt;
1428         gint salt_len;
1429         gint cryptgrm_len;
1430         guint8* cryptgrm;
1431         tvbuff_t* clear_tvb;
1432         guint8 iv[8];
1433         guint i;
1434
1435
1436         salt_len = tvb_length_remaining(p->priv_tvb,0);
1437
1438         if (salt_len != 8)  {
1439                 *error = "decryptionError: msgPrivacyParameters length != 8";
1440                 return NULL;
1441         }
1442
1443         salt = ep_tvb_memdup(p->priv_tvb,0,salt_len);
1444
1445         /*
1446          The resulting "salt" is XOR-ed with the pre-IV to obtain the IV.
1447          */
1448         for (i=0; i<8; i++) {
1449                 iv[i] = pre_iv[i] ^ salt[i];
1450         }
1451
1452         cryptgrm_len = tvb_length_remaining(encryptedData,0);
1453
1454         if (cryptgrm_len % 8) {
1455                 *error = "decryptionError: the length of the encrypted data is not a mutiple of 8 octets";
1456                 return NULL;
1457         }
1458
1459         cryptgrm = ep_tvb_memdup(encryptedData,0,-1);
1460
1461         cleartext = ep_alloc(cryptgrm_len);
1462
1463         err = gcry_cipher_open(&hd, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_CBC, 0);
1464         if (err != GPG_ERR_NO_ERROR) goto on_gcry_error;
1465
1466         err = gcry_cipher_setiv(hd, iv, 8);
1467         if (err != GPG_ERR_NO_ERROR) goto on_gcry_error;
1468
1469         err = gcry_cipher_setkey(hd,des_key,8);
1470         if (err != GPG_ERR_NO_ERROR) goto on_gcry_error;
1471
1472         err = gcry_cipher_decrypt(hd, cleartext, cryptgrm_len, cryptgrm, cryptgrm_len);
1473         if (err != GPG_ERR_NO_ERROR) goto on_gcry_error;
1474
1475         gcry_cipher_close(hd);
1476
1477         clear_tvb = tvb_new_child_real_data(encryptedData, cleartext, cryptgrm_len, cryptgrm_len);
1478
1479         return clear_tvb;
1480
1481 on_gcry_error:
1482         *error = (void*)gpg_strerror(err);
1483         if (hd) gcry_cipher_close(hd);
1484         return NULL;
1485 #else
1486         *error = "libgcrypt not present, cannot decrypt";
1487         return NULL;
1488 #endif
1489 }
1490
1491 static tvbuff_t*
1492 snmp_usm_priv_aes(snmp_usm_params_t* p _U_, tvbuff_t* encryptedData _U_, gchar const** error _U_)
1493 {
1494 #ifdef HAVE_LIBGCRYPT
1495         gcry_error_t err;
1496         gcry_cipher_hd_t hd = NULL;
1497
1498         guint8* cleartext;
1499         guint8* aes_key = p->user_assoc->user.privKey.data; /* first 16 bytes */
1500         guint8 iv[16];
1501         gint priv_len;
1502         gint cryptgrm_len;
1503         guint8* cryptgrm;
1504         tvbuff_t* clear_tvb;
1505
1506         priv_len = tvb_length_remaining(p->priv_tvb,0);
1507
1508         if (priv_len != 8)  {
1509                 *error = "decryptionError: msgPrivacyParameters length != 8";
1510                 return NULL;
1511         }
1512
1513         iv[0] = (p->boots & 0xff000000) >> 24;
1514         iv[1] = (p->boots & 0x00ff0000) >> 16;
1515         iv[2] = (p->boots & 0x0000ff00) >> 8;
1516         iv[3] = (p->boots & 0x000000ff);
1517         iv[4] = (p->time & 0xff000000) >> 24;
1518         iv[5] = (p->time & 0x00ff0000) >> 16;
1519         iv[6] = (p->time & 0x0000ff00) >> 8;
1520         iv[7] = (p->time & 0x000000ff);
1521         tvb_memcpy(p->priv_tvb,&(iv[8]),0,8);
1522
1523         cryptgrm_len = tvb_length_remaining(encryptedData,0);
1524         cryptgrm = ep_tvb_memdup(encryptedData,0,-1);
1525
1526         cleartext = ep_alloc(cryptgrm_len);
1527
1528         err = gcry_cipher_open(&hd, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CFB, 0);
1529         if (err != GPG_ERR_NO_ERROR) goto on_gcry_error;
1530
1531         err = gcry_cipher_setiv(hd, iv, 16);
1532         if (err != GPG_ERR_NO_ERROR) goto on_gcry_error;
1533
1534         err = gcry_cipher_setkey(hd,aes_key,16);
1535         if (err != GPG_ERR_NO_ERROR) goto on_gcry_error;
1536
1537         err = gcry_cipher_decrypt(hd, cleartext, cryptgrm_len, cryptgrm, cryptgrm_len);
1538         if (err != GPG_ERR_NO_ERROR) goto on_gcry_error;
1539
1540         gcry_cipher_close(hd);
1541
1542         clear_tvb = tvb_new_child_real_data(encryptedData, cleartext, cryptgrm_len, cryptgrm_len);
1543
1544         return clear_tvb;
1545
1546 on_gcry_error:
1547         *error = (void*)gpg_strerror(err);
1548         if (hd) gcry_cipher_close(hd);
1549         return NULL;
1550 #else
1551         *error = "libgcrypt not present, cannot decrypt";
1552         return NULL;
1553 #endif
1554 }
1555
1556
1557 gboolean
1558 check_ScopedPdu(tvbuff_t* tvb)
1559 {
1560         int offset;
1561         gint8 class;
1562         gboolean pc;
1563         gint32 tag;
1564         int hoffset, eoffset;
1565         guint32 len;
1566
1567         offset = get_ber_identifier(tvb, 0, &class, &pc, &tag);
1568         offset = get_ber_length(tvb, offset, NULL, NULL);
1569
1570         if ( ! (((class!=BER_CLASS_APP) && (class!=BER_CLASS_PRI) )
1571                         && ( (!pc) || (class!=BER_CLASS_UNI) || (tag!=BER_UNI_TAG_ENUMERATED) )
1572                         )) return FALSE;
1573
1574         if((tvb_get_guint8(tvb, offset)==0)&&(tvb_get_guint8(tvb, offset+1)==0))
1575                 return TRUE;
1576
1577         hoffset = offset;
1578
1579         offset = get_ber_identifier(tvb, offset, &class, &pc, &tag);
1580         offset = get_ber_length(tvb, offset, &len, NULL);
1581         eoffset = offset + len;
1582
1583         if (eoffset <= hoffset) return FALSE;
1584
1585         if ((class!=BER_CLASS_APP)&&(class!=BER_CLASS_PRI))
1586                 if( (class!=BER_CLASS_UNI)
1587                         ||((tag<BER_UNI_TAG_NumericString)&&(tag!=BER_UNI_TAG_OCTETSTRING)&&(tag!=BER_UNI_TAG_UTF8String)) )
1588                         return FALSE;
1589
1590         return TRUE;
1591
1592 }
1593
1594
1595 /*--- Included file: packet-snmp-fn.c ---*/
1596 #line 1 "packet-snmp-fn.c"
1597
1598
1599
1600 static int
1601 dissect_snmp_EnterpriseOID(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1602 #line 64 "snmp.cnf"
1603   const gchar* name;
1604
1605   offset = dissect_ber_object_identifier_str(implicit_tag, actx, tree, tvb, offset, hf_index, &enterprise_oid);
1606
1607
1608   if (display_oid && enterprise_oid) {
1609     name = oid_resolved_from_string(enterprise_oid);
1610     if (name) {
1611       col_append_fstr (actx->pinfo->cinfo, COL_INFO, " %s", name);
1612     }
1613   }
1614
1615
1616
1617   return offset;
1618 }
1619
1620
1621
1622 static int
1623 dissect_snmp_OCTET_STRING_SIZE_4(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1624   offset = dissect_ber_octet_string(implicit_tag, actx, tree, tvb, offset, hf_index,
1625                                        NULL);
1626
1627   return offset;
1628 }
1629
1630
1631
1632 static int
1633 dissect_snmp_NetworkAddress(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1634   offset = dissect_ber_tagged_type(implicit_tag, actx, tree, tvb, offset,
1635                                       hf_index, BER_CLASS_APP, 0, TRUE, dissect_snmp_OCTET_STRING_SIZE_4);
1636
1637   return offset;
1638 }
1639
1640
1641
1642 static int
1643 dissect_snmp_INTEGER_0_4294967295(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1644   offset = dissect_ber_integer(implicit_tag, actx, tree, tvb, offset, hf_index,
1645                                                 NULL);
1646
1647   return offset;
1648 }
1649
1650
1651
1652 static int
1653 dissect_snmp_TimeTicks(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1654   offset = dissect_ber_tagged_type(implicit_tag, actx, tree, tvb, offset,
1655                                       hf_index, BER_CLASS_APP, 3, TRUE, dissect_snmp_INTEGER_0_4294967295);
1656
1657   return offset;
1658 }
1659
1660
1661
1662 static int
1663 dissect_snmp_Integer32(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1664   offset = dissect_ber_integer(implicit_tag, actx, tree, tvb, offset, hf_index,
1665                                                 NULL);
1666
1667   return offset;
1668 }
1669
1670
1671
1672 static int
1673 dissect_snmp_ObjectName(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1674   offset = dissect_ber_object_identifier(implicit_tag, actx, tree, tvb, offset, hf_index, NULL);
1675
1676   return offset;
1677 }
1678
1679
1680 static const value_string snmp_Version_vals[] = {
1681   {   0, "version-1" },
1682   {   1, "v2c" },
1683   {   2, "v2u" },
1684   {   3, "snmpv3" },
1685   { 0, NULL }
1686 };
1687
1688
1689 static int
1690 dissect_snmp_Version(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1691   offset = dissect_ber_integer(implicit_tag, actx, tree, tvb, offset, hf_index,
1692                                                 NULL);
1693
1694   return offset;
1695 }
1696
1697
1698
1699 static int
1700 dissect_snmp_Community(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1701   offset = dissect_ber_octet_string(implicit_tag, actx, tree, tvb, offset, hf_index,
1702                                        NULL);
1703
1704   return offset;
1705 }
1706
1707
1708
1709 static int
1710 dissect_snmp_INTEGER(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1711   offset = dissect_ber_integer(implicit_tag, actx, tree, tvb, offset, hf_index,
1712                                                 NULL);
1713
1714   return offset;
1715 }
1716
1717
1718 static const value_string snmp_T_error_status_vals[] = {
1719   {   0, "noError" },
1720   {   1, "tooBig" },
1721   {   2, "noSuchName" },
1722   {   3, "badValue" },
1723   {   4, "readOnly" },
1724   {   5, "genErr" },
1725   {   6, "noAccess" },
1726   {   7, "wrongType" },
1727   {   8, "wrongLength" },
1728   {   9, "wrongEncoding" },
1729   {  10, "wrongValue" },
1730   {  11, "noCreation" },
1731   {  12, "inconsistentValue" },
1732   {  13, "resourceUnavailable" },
1733   {  14, "commitFailed" },
1734   {  15, "undoFailed" },
1735   {  16, "authorizationError" },
1736   {  17, "notWritable" },
1737   {  18, "inconsistentName" },
1738   { 0, NULL }
1739 };
1740
1741
1742 static int
1743 dissect_snmp_T_error_status(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1744   offset = dissect_ber_integer(implicit_tag, actx, tree, tvb, offset, hf_index,
1745                                                 NULL);
1746
1747   return offset;
1748 }
1749
1750
1751
1752 static int
1753 dissect_snmp_NULL(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1754   offset = dissect_ber_null(implicit_tag, actx, tree, tvb, offset, hf_index);
1755
1756   return offset;
1757 }
1758
1759
1760
1761 static const ber_sequence_t VarBindList_sequence_of[1] = {
1762   { &hf_snmp_VarBindList_item, BER_CLASS_UNI, BER_UNI_TAG_SEQUENCE, BER_FLAGS_NOOWNTAG, dissect_snmp_VarBind },
1763 };
1764
1765 static int
1766 dissect_snmp_VarBindList(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1767   offset = dissect_ber_sequence_of(implicit_tag, actx, tree, tvb, offset,
1768                                       VarBindList_sequence_of, hf_index, ett_snmp_VarBindList);
1769
1770   return offset;
1771 }
1772
1773
1774 static const ber_sequence_t PDU_sequence[] = {
1775   { &hf_snmp_request_id     , BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_snmp_INTEGER },
1776   { &hf_snmp_error_status   , BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_snmp_T_error_status },
1777   { &hf_snmp_error_index    , BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_snmp_INTEGER },
1778   { &hf_snmp_variable_bindings, BER_CLASS_UNI, BER_UNI_TAG_SEQUENCE, BER_FLAGS_NOOWNTAG, dissect_snmp_VarBindList },
1779   { NULL, 0, 0, 0, NULL }
1780 };
1781
1782 static int
1783 dissect_snmp_PDU(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1784   offset = dissect_ber_sequence(implicit_tag, actx, tree, tvb, offset,
1785                                    PDU_sequence, hf_index, ett_snmp_PDU);
1786
1787   return offset;
1788 }
1789
1790
1791
1792 static int
1793 dissect_snmp_GetRequest_PDU(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1794   offset = dissect_ber_tagged_type(implicit_tag, actx, tree, tvb, offset,
1795                                       hf_index, BER_CLASS_CON, 0, TRUE, dissect_snmp_PDU);
1796
1797   return offset;
1798 }
1799
1800
1801
1802 static int
1803 dissect_snmp_GetNextRequest_PDU(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1804   offset = dissect_ber_tagged_type(implicit_tag, actx, tree, tvb, offset,
1805                                       hf_index, BER_CLASS_CON, 1, TRUE, dissect_snmp_PDU);
1806
1807   return offset;
1808 }
1809
1810
1811
1812 static int
1813 dissect_snmp_GetResponse_PDU(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1814   offset = dissect_ber_tagged_type(implicit_tag, actx, tree, tvb, offset,
1815                                       hf_index, BER_CLASS_CON, 2, TRUE, dissect_snmp_PDU);
1816
1817   return offset;
1818 }
1819
1820
1821
1822 static int
1823 dissect_snmp_SetRequest_PDU(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1824   offset = dissect_ber_tagged_type(implicit_tag, actx, tree, tvb, offset,
1825                                       hf_index, BER_CLASS_CON, 3, TRUE, dissect_snmp_PDU);
1826
1827   return offset;
1828 }
1829
1830
1831 static const value_string snmp_GenericTrap_vals[] = {
1832   {   0, "coldStart" },
1833   {   1, "warmStart" },
1834   {   2, "linkDown" },
1835   {   3, "linkUp" },
1836   {   4, "authenticationFailure" },
1837   {   5, "egpNeighborLoss" },
1838   {   6, "enterpriseSpecific" },
1839   { 0, NULL }
1840 };
1841
1842
1843 static int
1844 dissect_snmp_GenericTrap(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1845   offset = dissect_ber_integer(implicit_tag, actx, tree, tvb, offset, hf_index,
1846                                                 &generic_trap);
1847
1848   return offset;
1849 }
1850
1851
1852
1853 static int
1854 dissect_snmp_SpecificTrap(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1855 #line 48 "snmp.cnf"
1856   guint specific_trap;
1857   
1858   offset = dissect_ber_integer(implicit_tag, actx, tree, tvb, offset, hf_index,
1859                                                 &specific_trap);
1860
1861
1862   if (generic_trap == 6) { /* enterprise specific */
1863     const gchar *specific_str = snmp_lookup_specific_trap (specific_trap);
1864     if (specific_str) {
1865       proto_item_append_text(actx->created_item, " (%s)", specific_str);
1866     }
1867   }
1868
1869
1870   return offset;
1871 }
1872
1873
1874 static const ber_sequence_t Trap_PDU_U_sequence[] = {
1875   { &hf_snmp_enterprise     , BER_CLASS_UNI, BER_UNI_TAG_OID, BER_FLAGS_NOOWNTAG, dissect_snmp_EnterpriseOID },
1876   { &hf_snmp_agent_addr     , BER_CLASS_APP, 0, BER_FLAGS_NOOWNTAG, dissect_snmp_NetworkAddress },
1877   { &hf_snmp_generic_trap   , BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_snmp_GenericTrap },
1878   { &hf_snmp_specific_trap  , BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_snmp_SpecificTrap },
1879   { &hf_snmp_time_stamp     , BER_CLASS_APP, 3, BER_FLAGS_NOOWNTAG, dissect_snmp_TimeTicks },
1880   { &hf_snmp_variable_bindings, BER_CLASS_UNI, BER_UNI_TAG_SEQUENCE, BER_FLAGS_NOOWNTAG, dissect_snmp_VarBindList },
1881   { NULL, 0, 0, 0, NULL }
1882 };
1883
1884 static int
1885 dissect_snmp_Trap_PDU_U(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1886 #line 40 "snmp.cnf"
1887   generic_trap = 0;
1888   enterprise_oid = NULL;
1889
1890   offset = dissect_ber_sequence(implicit_tag, actx, tree, tvb, offset,
1891                                    Trap_PDU_U_sequence, hf_index, ett_snmp_Trap_PDU_U);
1892
1893
1894
1895
1896   return offset;
1897 }
1898
1899
1900
1901 static int
1902 dissect_snmp_Trap_PDU(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1903   offset = dissect_ber_tagged_type(implicit_tag, actx, tree, tvb, offset,
1904                                       hf_index, BER_CLASS_CON, 4, TRUE, dissect_snmp_Trap_PDU_U);
1905
1906   return offset;
1907 }
1908
1909
1910
1911 static int
1912 dissect_snmp_INTEGER_0_2147483647(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1913   offset = dissect_ber_integer(implicit_tag, actx, tree, tvb, offset, hf_index,
1914                                                 NULL);
1915
1916   return offset;
1917 }
1918
1919
1920 static const ber_sequence_t BulkPDU_sequence[] = {
1921   { &hf_snmp_bulkPDU_request_id, BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_snmp_Integer32 },
1922   { &hf_snmp_non_repeaters  , BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_snmp_INTEGER_0_2147483647 },
1923   { &hf_snmp_max_repetitions, BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_snmp_INTEGER_0_2147483647 },
1924   { &hf_snmp_variable_bindings, BER_CLASS_UNI, BER_UNI_TAG_SEQUENCE, BER_FLAGS_NOOWNTAG, dissect_snmp_VarBindList },
1925   { NULL, 0, 0, 0, NULL }
1926 };
1927
1928 static int
1929 dissect_snmp_BulkPDU(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1930   offset = dissect_ber_sequence(implicit_tag, actx, tree, tvb, offset,
1931                                    BulkPDU_sequence, hf_index, ett_snmp_BulkPDU);
1932
1933   return offset;
1934 }
1935
1936
1937
1938 static int
1939 dissect_snmp_GetBulkRequest_PDU(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1940   offset = dissect_ber_tagged_type(implicit_tag, actx, tree, tvb, offset,
1941                                       hf_index, BER_CLASS_CON, 5, TRUE, dissect_snmp_BulkPDU);
1942
1943   return offset;
1944 }
1945
1946
1947
1948 static int
1949 dissect_snmp_InformRequest_PDU(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1950   offset = dissect_ber_tagged_type(implicit_tag, actx, tree, tvb, offset,
1951                                       hf_index, BER_CLASS_CON, 6, TRUE, dissect_snmp_PDU);
1952
1953   return offset;
1954 }
1955
1956
1957
1958 static int
1959 dissect_snmp_SNMPv2_Trap_PDU(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1960   offset = dissect_ber_tagged_type(implicit_tag, actx, tree, tvb, offset,
1961                                       hf_index, BER_CLASS_CON, 7, TRUE, dissect_snmp_PDU);
1962
1963   return offset;
1964 }
1965
1966
1967
1968 static int
1969 dissect_snmp_Report_PDU(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
1970   offset = dissect_ber_tagged_type(implicit_tag, actx, tree, tvb, offset,
1971                                       hf_index, BER_CLASS_CON, 8, TRUE, dissect_snmp_PDU);
1972
1973   return offset;
1974 }
1975
1976
1977 static const value_string snmp_PDUs_vals[] = {
1978   {   0, "get-request" },
1979   {   1, "get-next-request" },
1980   {   2, "get-response" },
1981   {   3, "set-request" },
1982   {   4, "trap" },
1983   {   5, "getBulkRequest" },
1984   {   6, "informRequest" },
1985   {   7, "sNMPv2-Trap" },
1986   {   8, "report" },
1987   { 0, NULL }
1988 };
1989
1990 static const ber_choice_t PDUs_choice[] = {
1991   {   0, &hf_snmp_get_request    , BER_CLASS_CON, 0, BER_FLAGS_NOOWNTAG, dissect_snmp_GetRequest_PDU },
1992   {   1, &hf_snmp_get_next_request, BER_CLASS_CON, 1, BER_FLAGS_NOOWNTAG, dissect_snmp_GetNextRequest_PDU },
1993   {   2, &hf_snmp_get_response   , BER_CLASS_CON, 2, BER_FLAGS_NOOWNTAG, dissect_snmp_GetResponse_PDU },
1994   {   3, &hf_snmp_set_request    , BER_CLASS_CON, 3, BER_FLAGS_NOOWNTAG, dissect_snmp_SetRequest_PDU },
1995   {   4, &hf_snmp_trap           , BER_CLASS_CON, 4, BER_FLAGS_NOOWNTAG, dissect_snmp_Trap_PDU },
1996   {   5, &hf_snmp_getBulkRequest , BER_CLASS_CON, 5, BER_FLAGS_NOOWNTAG, dissect_snmp_GetBulkRequest_PDU },
1997   {   6, &hf_snmp_informRequest  , BER_CLASS_CON, 6, BER_FLAGS_NOOWNTAG, dissect_snmp_InformRequest_PDU },
1998   {   7, &hf_snmp_sNMPv2_Trap    , BER_CLASS_CON, 7, BER_FLAGS_NOOWNTAG, dissect_snmp_SNMPv2_Trap_PDU },
1999   {   8, &hf_snmp_report         , BER_CLASS_CON, 8, BER_FLAGS_NOOWNTAG, dissect_snmp_Report_PDU },
2000   { 0, NULL, 0, 0, 0, NULL }
2001 };
2002
2003 static int
2004 dissect_snmp_PDUs(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2005 #line 28 "snmp.cnf"
2006 gint pdu_type=-1;
2007
2008   col_clear(actx->pinfo->cinfo, COL_INFO);
2009
2010   offset = dissect_ber_choice(actx, tree, tvb, offset,
2011                                  PDUs_choice, hf_index, ett_snmp_PDUs,
2012                                  &pdu_type);
2013
2014   if( (pdu_type!=-1) && snmp_PDUs_vals[pdu_type].strptr ){
2015         col_prepend_fstr(actx->pinfo->cinfo, COL_INFO, "%s", snmp_PDUs_vals[pdu_type].strptr);
2016   }
2017
2018
2019
2020   return offset;
2021 }
2022
2023
2024 static const ber_sequence_t Message_sequence[] = {
2025   { &hf_snmp_version        , BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_snmp_Version },
2026   { &hf_snmp_community      , BER_CLASS_UNI, BER_UNI_TAG_OCTETSTRING, BER_FLAGS_NOOWNTAG, dissect_snmp_Community },
2027   { &hf_snmp_data           , BER_CLASS_ANY/*choice*/, -1/*choice*/, BER_FLAGS_NOOWNTAG|BER_FLAGS_NOTCHKTAG, dissect_snmp_PDUs },
2028   { NULL, 0, 0, 0, NULL }
2029 };
2030
2031 static int
2032 dissect_snmp_Message(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2033   offset = dissect_ber_sequence(implicit_tag, actx, tree, tvb, offset,
2034                                    Message_sequence, hf_index, ett_snmp_Message);
2035
2036   return offset;
2037 }
2038
2039
2040
2041 static int
2042 dissect_snmp_OCTET_STRING(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2043   offset = dissect_ber_octet_string(implicit_tag, actx, tree, tvb, offset, hf_index,
2044                                        NULL);
2045
2046   return offset;
2047 }
2048
2049
2050 static const value_string snmp_T_datav2u_vals[] = {
2051   {   0, "plaintext" },
2052   {   1, "encrypted" },
2053   { 0, NULL }
2054 };
2055
2056 static const ber_choice_t T_datav2u_choice[] = {
2057   {   0, &hf_snmp_v2u_plaintext  , BER_CLASS_ANY/*choice*/, -1/*choice*/, BER_FLAGS_NOOWNTAG, dissect_snmp_PDUs },
2058   {   1, &hf_snmp_encrypted      , BER_CLASS_UNI, BER_UNI_TAG_OCTETSTRING, BER_FLAGS_NOOWNTAG, dissect_snmp_OCTET_STRING },
2059   { 0, NULL, 0, 0, 0, NULL }
2060 };
2061
2062 static int
2063 dissect_snmp_T_datav2u(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2064   offset = dissect_ber_choice(actx, tree, tvb, offset,
2065                                  T_datav2u_choice, hf_index, ett_snmp_T_datav2u,
2066                                  NULL);
2067
2068   return offset;
2069 }
2070
2071
2072 static const ber_sequence_t Messagev2u_sequence[] = {
2073   { &hf_snmp_version        , BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_snmp_Version },
2074   { &hf_snmp_parameters     , BER_CLASS_UNI, BER_UNI_TAG_OCTETSTRING, BER_FLAGS_NOOWNTAG, dissect_snmp_OCTET_STRING },
2075   { &hf_snmp_datav2u        , BER_CLASS_ANY/*choice*/, -1/*choice*/, BER_FLAGS_NOOWNTAG|BER_FLAGS_NOTCHKTAG, dissect_snmp_T_datav2u },
2076   { NULL, 0, 0, 0, NULL }
2077 };
2078
2079 static int
2080 dissect_snmp_Messagev2u(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2081   offset = dissect_ber_sequence(implicit_tag, actx, tree, tvb, offset,
2082                                    Messagev2u_sequence, hf_index, ett_snmp_Messagev2u);
2083
2084   return offset;
2085 }
2086
2087
2088
2089 static int
2090 dissect_snmp_SnmpEngineID(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2091 #line 99 "snmp.cnf"
2092         tvbuff_t* param_tvb = NULL;
2093
2094         offset = dissect_ber_octet_string(implicit_tag, actx, tree, tvb, offset, hf_index,
2095                                        &param_tvb);
2096          if (param_tvb) {
2097                 proto_tree* engine_tree = proto_item_add_subtree(actx->created_item,ett_engineid);
2098                 dissect_snmp_engineid(engine_tree, param_tvb, 0, tvb_length_remaining(param_tvb,0));
2099         }
2100
2101
2102
2103   return offset;
2104 }
2105
2106
2107
2108 static int
2109 dissect_snmp_T_msgAuthoritativeEngineID(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2110 #line 90 "snmp.cnf"
2111
2112   offset = dissect_ber_octet_string(implicit_tag, actx, tree, tvb, offset, hf_index,
2113                                        &usm_p.engine_tvb);
2114          if (usm_p.engine_tvb) {
2115                 proto_tree* engine_tree = proto_item_add_subtree(actx->created_item,ett_engineid);
2116                 dissect_snmp_engineid(engine_tree, usm_p.engine_tvb, 0, tvb_length_remaining(usm_p.engine_tvb,0));
2117         }
2118
2119
2120
2121   return offset;
2122 }
2123
2124
2125
2126 static int
2127 dissect_snmp_T_msgAuthoritativeEngineBoots(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2128   offset = dissect_ber_integer(implicit_tag, actx, tree, tvb, offset, hf_index,
2129                                                 &usm_p.boots);
2130
2131   return offset;
2132 }
2133
2134
2135
2136 static int
2137 dissect_snmp_T_msgAuthoritativeEngineTime(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2138   offset = dissect_ber_integer(implicit_tag, actx, tree, tvb, offset, hf_index,
2139                                                 &usm_p.time);
2140
2141   return offset;
2142 }
2143
2144
2145
2146 static int
2147 dissect_snmp_T_msgUserName(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2148   offset = dissect_ber_octet_string(implicit_tag, actx, tree, tvb, offset, hf_index,
2149                                        &usm_p.user_tvb);
2150
2151   return offset;
2152 }
2153
2154
2155
2156 static int
2157 dissect_snmp_T_msgAuthenticationParameters(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2158 #line 112 "snmp.cnf"
2159         offset = dissect_ber_octet_string(FALSE, actx, tree, tvb, offset, hf_index, &usm_p.auth_tvb);
2160         if (usm_p.auth_tvb) {
2161                 usm_p.auth_item = actx->created_item;
2162                 usm_p.auth_offset = tvb_offset_from_real_beginning(usm_p.auth_tvb);
2163         }
2164
2165
2166   return offset;
2167 }
2168
2169
2170
2171 static int
2172 dissect_snmp_T_msgPrivacyParameters(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2173   offset = dissect_ber_octet_string(implicit_tag, actx, tree, tvb, offset, hf_index,
2174                                        &usm_p.priv_tvb);
2175
2176   return offset;
2177 }
2178
2179
2180 static const ber_sequence_t UsmSecurityParameters_sequence[] = {
2181   { &hf_snmp_msgAuthoritativeEngineID, BER_CLASS_UNI, BER_UNI_TAG_OCTETSTRING, BER_FLAGS_NOOWNTAG, dissect_snmp_T_msgAuthoritativeEngineID },
2182   { &hf_snmp_msgAuthoritativeEngineBoots, BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_snmp_T_msgAuthoritativeEngineBoots },
2183   { &hf_snmp_msgAuthoritativeEngineTime, BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_snmp_T_msgAuthoritativeEngineTime },
2184   { &hf_snmp_msgUserName    , BER_CLASS_UNI, BER_UNI_TAG_OCTETSTRING, BER_FLAGS_NOOWNTAG, dissect_snmp_T_msgUserName },
2185   { &hf_snmp_msgAuthenticationParameters, BER_CLASS_UNI, BER_UNI_TAG_OCTETSTRING, BER_FLAGS_NOOWNTAG, dissect_snmp_T_msgAuthenticationParameters },
2186   { &hf_snmp_msgPrivacyParameters, BER_CLASS_UNI, BER_UNI_TAG_OCTETSTRING, BER_FLAGS_NOOWNTAG, dissect_snmp_T_msgPrivacyParameters },
2187   { NULL, 0, 0, 0, NULL }
2188 };
2189
2190 static int
2191 dissect_snmp_UsmSecurityParameters(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2192   offset = dissect_ber_sequence(implicit_tag, actx, tree, tvb, offset,
2193                                    UsmSecurityParameters_sequence, hf_index, ett_snmp_UsmSecurityParameters);
2194
2195   return offset;
2196 }
2197
2198
2199
2200 static int
2201 dissect_snmp_INTEGER_484_2147483647(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2202   offset = dissect_ber_integer(implicit_tag, actx, tree, tvb, offset, hf_index,
2203                                                 NULL);
2204
2205   return offset;
2206 }
2207
2208
2209
2210 static int
2211 dissect_snmp_T_msgFlags(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2212 #line 227 "snmp.cnf"
2213         tvbuff_t *parameter_tvb = NULL;
2214
2215    offset = dissect_ber_octet_string(implicit_tag, actx, tree, tvb, offset, hf_index,
2216                                        &parameter_tvb);
2217
2218  if (parameter_tvb){
2219         guint8 v3_flags = tvb_get_guint8(parameter_tvb, 0);
2220         proto_tree* flags_tree = proto_item_add_subtree(actx->created_item,ett_msgFlags);
2221         
2222         proto_tree_add_item(flags_tree, hf_snmp_v3_flags_report, parameter_tvb, 0, 1, FALSE);
2223         proto_tree_add_item(flags_tree, hf_snmp_v3_flags_crypt, parameter_tvb, 0, 1, FALSE);
2224         proto_tree_add_item(flags_tree, hf_snmp_v3_flags_auth, parameter_tvb, 0, 1, FALSE);
2225         
2226         usm_p.encrypted = v3_flags & TH_CRYPT ? TRUE : FALSE;
2227         usm_p.authenticated = v3_flags & TH_AUTH ? TRUE : FALSE;
2228   }
2229
2230
2231
2232
2233   return offset;
2234 }
2235
2236
2237
2238 static int
2239 dissect_snmp_T_msgSecurityModel(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2240   offset = dissect_ber_integer(implicit_tag, actx, tree, tvb, offset, hf_index,
2241                                                 &MsgSecurityModel);
2242
2243   return offset;
2244 }
2245
2246
2247 static const ber_sequence_t HeaderData_sequence[] = {
2248   { &hf_snmp_msgID          , BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_snmp_INTEGER_0_2147483647 },
2249   { &hf_snmp_msgMaxSize     , BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_snmp_INTEGER_484_2147483647 },
2250   { &hf_snmp_msgFlags       , BER_CLASS_UNI, BER_UNI_TAG_OCTETSTRING, BER_FLAGS_NOOWNTAG, dissect_snmp_T_msgFlags },
2251   { &hf_snmp_msgSecurityModel, BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_snmp_T_msgSecurityModel },
2252   { NULL, 0, 0, 0, NULL }
2253 };
2254
2255 static int
2256 dissect_snmp_HeaderData(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2257   offset = dissect_ber_sequence(implicit_tag, actx, tree, tvb, offset,
2258                                    HeaderData_sequence, hf_index, ett_snmp_HeaderData);
2259
2260   return offset;
2261 }
2262
2263
2264
2265 static int
2266 dissect_snmp_T_msgSecurityParameters(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2267 #line 170 "snmp.cnf"
2268
2269         switch(MsgSecurityModel){
2270                 case SNMP_SEC_USM:      /* 3 */         
2271                         offset = dissect_snmp_UsmSecurityParameters(FALSE, tvb, offset+2, actx, tree, -1);
2272                         usm_p.user_assoc = get_user_assoc(usm_p.engine_tvb, usm_p.user_tvb);
2273                         break;
2274                 case SNMP_SEC_ANY:      /* 0 */
2275                 case SNMP_SEC_V1:       /* 1 */
2276                 case SNMP_SEC_V2C:      /* 2 */
2277                 default:
2278                           offset = dissect_ber_octet_string(implicit_tag, actx, tree, tvb, offset, hf_index,
2279                                        NULL);
2280
2281                         break;
2282         }
2283
2284
2285
2286   return offset;
2287 }
2288
2289
2290 static const ber_sequence_t ScopedPDU_sequence[] = {
2291   { &hf_snmp_contextEngineID, BER_CLASS_UNI, BER_UNI_TAG_OCTETSTRING, BER_FLAGS_NOOWNTAG, dissect_snmp_SnmpEngineID },
2292   { &hf_snmp_contextName    , BER_CLASS_UNI, BER_UNI_TAG_OCTETSTRING, BER_FLAGS_NOOWNTAG, dissect_snmp_OCTET_STRING },
2293   { &hf_snmp_data           , BER_CLASS_ANY/*choice*/, -1/*choice*/, BER_FLAGS_NOOWNTAG|BER_FLAGS_NOTCHKTAG, dissect_snmp_PDUs },
2294   { NULL, 0, 0, 0, NULL }
2295 };
2296
2297 static int
2298 dissect_snmp_ScopedPDU(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2299   offset = dissect_ber_sequence(implicit_tag, actx, tree, tvb, offset,
2300                                    ScopedPDU_sequence, hf_index, ett_snmp_ScopedPDU);
2301
2302   return offset;
2303 }
2304
2305
2306
2307 static int
2308 dissect_snmp_T_encryptedPDU(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2309 #line 121 "snmp.cnf"
2310         tvbuff_t* crypt_tvb;
2311         offset = dissect_ber_octet_string(FALSE, actx, tree, tvb, offset, hf_snmp_encryptedPDU, &crypt_tvb);
2312
2313         if( usm_p.encrypted && crypt_tvb
2314                 && usm_p.user_assoc
2315                 && usm_p.user_assoc->user.privProtocol ) {
2316                 
2317                 const gchar* error = NULL;
2318                 proto_tree* encryptedpdu_tree = proto_item_add_subtree(actx->created_item,ett_encryptedPDU);
2319                 tvbuff_t* cleartext_tvb = usm_p.user_assoc->user.privProtocol(&usm_p, crypt_tvb, &error );
2320
2321                 if (! cleartext_tvb) {
2322                         proto_item* cause = proto_tree_add_text(encryptedpdu_tree, crypt_tvb, 0, -1,
2323                                 "Failed to decrypt encryptedPDU: %s", error);
2324                         
2325                         expert_add_info_format(actx->pinfo, cause, PI_MALFORMED, PI_WARN,
2326                                 "Failed to decrypt encryptedPDU: %s", error);
2327
2328                         col_set_str(actx->pinfo->cinfo, COL_INFO, "encryptedPDU: Failed to decrypt");
2329                                 
2330                         return offset;
2331                 } else {
2332                         proto_item* decrypted_item;
2333                         proto_tree* decrypted_tree;
2334
2335                         if (! check_ScopedPdu(cleartext_tvb)) {
2336                                 proto_item* cause = proto_tree_add_text(encryptedpdu_tree, cleartext_tvb, 0, -1,
2337                                                                                         "Decrypted data not formatted as expected, wrong key?");
2338                                 
2339                                 expert_add_info_format(actx->pinfo, cause, PI_MALFORMED, PI_WARN,
2340                                                                            "Decrypted data not formatted as expected");
2341
2342                                 col_set_str(actx->pinfo->cinfo, COL_INFO, "encryptedPDU: Decrypted data not formatted as expected");
2343                                 
2344                                 return offset;
2345                         }
2346
2347                         
2348             add_new_data_source(actx->pinfo, cleartext_tvb, "Decrypted ScopedPDU");
2349                         
2350                         decrypted_item = proto_tree_add_item(encryptedpdu_tree, hf_snmp_decryptedPDU,cleartext_tvb,0,-1,FALSE);
2351                         decrypted_tree = proto_item_add_subtree(decrypted_item,ett_decrypted);
2352                         dissect_snmp_ScopedPDU(FALSE, cleartext_tvb, 0, actx, decrypted_tree, -1);
2353                  }
2354         } else {
2355                         col_set_str(actx->pinfo->cinfo, COL_INFO, "encryptedPDU: privKey Unknown");
2356         }
2357
2358
2359
2360   return offset;
2361 }
2362
2363
2364 static const value_string snmp_ScopedPduData_vals[] = {
2365   {   0, "plaintext" },
2366   {   1, "encryptedPDU" },
2367   { 0, NULL }
2368 };
2369
2370 static const ber_choice_t ScopedPduData_choice[] = {
2371   {   0, &hf_snmp_plaintext      , BER_CLASS_UNI, BER_UNI_TAG_SEQUENCE, BER_FLAGS_NOOWNTAG, dissect_snmp_ScopedPDU },
2372   {   1, &hf_snmp_encryptedPDU   , BER_CLASS_UNI, BER_UNI_TAG_OCTETSTRING, BER_FLAGS_NOOWNTAG, dissect_snmp_T_encryptedPDU },
2373   { 0, NULL, 0, 0, 0, NULL }
2374 };
2375
2376 static int
2377 dissect_snmp_ScopedPduData(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2378   offset = dissect_ber_choice(actx, tree, tvb, offset,
2379                                  ScopedPduData_choice, hf_index, ett_snmp_ScopedPduData,
2380                                  NULL);
2381
2382   return offset;
2383 }
2384
2385
2386 static const ber_sequence_t SNMPv3Message_sequence[] = {
2387   { &hf_snmp_msgVersion     , BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_snmp_Version },
2388   { &hf_snmp_msgGlobalData  , BER_CLASS_UNI, BER_UNI_TAG_SEQUENCE, BER_FLAGS_NOOWNTAG, dissect_snmp_HeaderData },
2389   { &hf_snmp_msgSecurityParameters, BER_CLASS_UNI, BER_UNI_TAG_OCTETSTRING, BER_FLAGS_NOOWNTAG, dissect_snmp_T_msgSecurityParameters },
2390   { &hf_snmp_msgData        , BER_CLASS_ANY/*choice*/, -1/*choice*/, BER_FLAGS_NOOWNTAG|BER_FLAGS_NOTCHKTAG, dissect_snmp_ScopedPduData },
2391   { NULL, 0, 0, 0, NULL }
2392 };
2393
2394 static int
2395 dissect_snmp_SNMPv3Message(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2396   offset = dissect_ber_sequence(implicit_tag, actx, tree, tvb, offset,
2397                                    SNMPv3Message_sequence, hf_index, ett_snmp_SNMPv3Message);
2398
2399 #line 185 "snmp.cnf"
2400
2401         if( usm_p.authenticated
2402                 && usm_p.user_assoc
2403                 && usm_p.user_assoc->user.authModel ) {
2404                 const gchar* error = NULL;
2405                 proto_item* authen_item;
2406                 proto_tree* authen_tree = proto_item_add_subtree(usm_p.auth_item,ett_authParameters);
2407                 guint8* calc_auth;
2408                 guint calc_auth_len;
2409                 
2410                 usm_p.authOK = usm_p.user_assoc->user.authModel->authenticate( &usm_p, &calc_auth, &calc_auth_len, &error );
2411
2412                 if (error) {
2413                         authen_item = proto_tree_add_text(authen_tree,tvb,0,0,"Error while verifying Message authenticity: %s", error);
2414                         PROTO_ITEM_SET_GENERATED(authen_item);
2415                         expert_add_info_format( actx->pinfo, authen_item, PI_MALFORMED, PI_ERROR, "Error while verifying Message authenticity: %s", error );
2416                 } else {
2417                         int severity;
2418                         gchar* msg;                     
2419
2420                         authen_item = proto_tree_add_boolean(authen_tree, hf_snmp_msgAuthentication, tvb, 0, 0, usm_p.authOK);
2421                         PROTO_ITEM_SET_GENERATED(authen_item);
2422                         
2423                         if (usm_p.authOK) {
2424                                 msg = "SNMP Authentication OK";
2425                                 severity = PI_CHAT;
2426                         } else {
2427                                 gchar* calc_auth_str = bytestring_to_str(calc_auth,calc_auth_len,' ');
2428                                 proto_item_append_text(authen_item, " calculated = %s", calc_auth_str);
2429                                 msg = "SNMP Authentication Error";
2430                                 severity = PI_WARN;
2431                         }
2432
2433                         expert_add_info_format( actx->pinfo, authen_item, PI_CHECKSUM, severity, "%s", msg );
2434                 }
2435         }
2436
2437
2438   return offset;
2439 }
2440
2441
2442 static const value_string snmp_T_smux_version_vals[] = {
2443   {   0, "version-1" },
2444   { 0, NULL }
2445 };
2446
2447
2448 static int
2449 dissect_snmp_T_smux_version(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2450   offset = dissect_ber_integer(implicit_tag, actx, tree, tvb, offset, hf_index,
2451                                                 NULL);
2452
2453   return offset;
2454 }
2455
2456
2457
2458 static int
2459 dissect_snmp_OBJECT_IDENTIFIER(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2460   offset = dissect_ber_object_identifier(implicit_tag, actx, tree, tvb, offset, hf_index, NULL);
2461
2462   return offset;
2463 }
2464
2465
2466
2467 static int
2468 dissect_snmp_DisplayString(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2469   offset = dissect_ber_octet_string(implicit_tag, actx, tree, tvb, offset, hf_index,
2470                                        NULL);
2471
2472   return offset;
2473 }
2474
2475
2476 static const ber_sequence_t SimpleOpen_U_sequence[] = {
2477   { &hf_snmp_smux_version   , BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_snmp_T_smux_version },
2478   { &hf_snmp_identity       , BER_CLASS_UNI, BER_UNI_TAG_OID, BER_FLAGS_NOOWNTAG, dissect_snmp_OBJECT_IDENTIFIER },
2479   { &hf_snmp_description    , BER_CLASS_UNI, BER_UNI_TAG_OCTETSTRING, BER_FLAGS_NOOWNTAG, dissect_snmp_DisplayString },
2480   { &hf_snmp_password       , BER_CLASS_UNI, BER_UNI_TAG_OCTETSTRING, BER_FLAGS_NOOWNTAG, dissect_snmp_OCTET_STRING },
2481   { NULL, 0, 0, 0, NULL }
2482 };
2483
2484 static int
2485 dissect_snmp_SimpleOpen_U(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2486   offset = dissect_ber_sequence(implicit_tag, actx, tree, tvb, offset,
2487                                    SimpleOpen_U_sequence, hf_index, ett_snmp_SimpleOpen_U);
2488
2489   return offset;
2490 }
2491
2492
2493
2494 static int
2495 dissect_snmp_SimpleOpen(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2496   offset = dissect_ber_tagged_type(implicit_tag, actx, tree, tvb, offset,
2497                                       hf_index, BER_CLASS_APP, 0, TRUE, dissect_snmp_SimpleOpen_U);
2498
2499   return offset;
2500 }
2501
2502
2503 static const value_string snmp_OpenPDU_vals[] = {
2504   {   0, "smux-simple" },
2505   { 0, NULL }
2506 };
2507
2508 static const ber_choice_t OpenPDU_choice[] = {
2509   {   0, &hf_snmp_smux_simple    , BER_CLASS_APP, 0, BER_FLAGS_NOOWNTAG, dissect_snmp_SimpleOpen },
2510   { 0, NULL, 0, 0, 0, NULL }
2511 };
2512
2513 static int
2514 dissect_snmp_OpenPDU(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2515   offset = dissect_ber_choice(actx, tree, tvb, offset,
2516                                  OpenPDU_choice, hf_index, ett_snmp_OpenPDU,
2517                                  NULL);
2518
2519   return offset;
2520 }
2521
2522
2523 static const value_string snmp_ClosePDU_U_vals[] = {
2524   {   0, "goingDown" },
2525   {   1, "unsupportedVersion" },
2526   {   2, "packetFormat" },
2527   {   3, "protocolError" },
2528   {   4, "internalError" },
2529   {   5, "authenticationFailure" },
2530   { 0, NULL }
2531 };
2532
2533
2534 static int
2535 dissect_snmp_ClosePDU_U(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2536   offset = dissect_ber_integer(implicit_tag, actx, tree, tvb, offset, hf_index,
2537                                                 NULL);
2538
2539   return offset;
2540 }
2541
2542
2543
2544 static int
2545 dissect_snmp_ClosePDU(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2546   offset = dissect_ber_tagged_type(implicit_tag, actx, tree, tvb, offset,
2547                                       hf_index, BER_CLASS_APP, 1, TRUE, dissect_snmp_ClosePDU_U);
2548
2549   return offset;
2550 }
2551
2552
2553
2554 static int
2555 dissect_snmp_INTEGER_M1_2147483647(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2556   offset = dissect_ber_integer(implicit_tag, actx, tree, tvb, offset, hf_index,
2557                                                 NULL);
2558
2559   return offset;
2560 }
2561
2562
2563 static const value_string snmp_T_operation_vals[] = {
2564   {   0, "delete" },
2565   {   1, "readOnly" },
2566   {   2, "readWrite" },
2567   { 0, NULL }
2568 };
2569
2570
2571 static int
2572 dissect_snmp_T_operation(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2573   offset = dissect_ber_integer(implicit_tag, actx, tree, tvb, offset, hf_index,
2574                                                 NULL);
2575
2576   return offset;
2577 }
2578
2579
2580 static const ber_sequence_t RReqPDU_U_sequence[] = {
2581   { &hf_snmp_subtree        , BER_CLASS_UNI, BER_UNI_TAG_OID, BER_FLAGS_NOOWNTAG, dissect_snmp_ObjectName },
2582   { &hf_snmp_priority       , BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_snmp_INTEGER_M1_2147483647 },
2583   { &hf_snmp_operation      , BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_snmp_T_operation },
2584   { NULL, 0, 0, 0, NULL }
2585 };
2586
2587 static int
2588 dissect_snmp_RReqPDU_U(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2589   offset = dissect_ber_sequence(implicit_tag, actx, tree, tvb, offset,
2590                                    RReqPDU_U_sequence, hf_index, ett_snmp_RReqPDU_U);
2591
2592   return offset;
2593 }
2594
2595
2596
2597 static int
2598 dissect_snmp_RReqPDU(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2599   offset = dissect_ber_tagged_type(implicit_tag, actx, tree, tvb, offset,
2600                                       hf_index, BER_CLASS_APP, 2, TRUE, dissect_snmp_RReqPDU_U);
2601
2602   return offset;
2603 }
2604
2605
2606 static const value_string snmp_RRspPDU_U_vals[] = {
2607   {  -1, "failure" },
2608   { 0, NULL }
2609 };
2610
2611
2612 static int
2613 dissect_snmp_RRspPDU_U(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2614   offset = dissect_ber_integer(implicit_tag, actx, tree, tvb, offset, hf_index,
2615                                                 NULL);
2616
2617   return offset;
2618 }
2619
2620
2621
2622 static int
2623 dissect_snmp_RRspPDU(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2624   offset = dissect_ber_tagged_type(implicit_tag, actx, tree, tvb, offset,
2625                                       hf_index, BER_CLASS_APP, 3, TRUE, dissect_snmp_RRspPDU_U);
2626
2627   return offset;
2628 }
2629
2630
2631 static const value_string snmp_RegisterResponse_vals[] = {
2632   {   0, "rRspPDU" },
2633   {   1, "pDUs" },
2634   { 0, NULL }
2635 };
2636
2637 static const ber_choice_t RegisterResponse_choice[] = {
2638   {   0, &hf_snmp_rRspPDU        , BER_CLASS_APP, 3, BER_FLAGS_NOOWNTAG, dissect_snmp_RRspPDU },
2639   {   1, &hf_snmp_pDUs           , BER_CLASS_ANY/*choice*/, -1/*choice*/, BER_FLAGS_NOOWNTAG, dissect_snmp_PDUs },
2640   { 0, NULL, 0, 0, 0, NULL }
2641 };
2642
2643 static int
2644 dissect_snmp_RegisterResponse(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2645   offset = dissect_ber_choice(actx, tree, tvb, offset,
2646                                  RegisterResponse_choice, hf_index, ett_snmp_RegisterResponse,
2647                                  NULL);
2648
2649   return offset;
2650 }
2651
2652
2653 static const value_string snmp_SOutPDU_U_vals[] = {
2654   {   0, "commit" },
2655   {   1, "rollback" },
2656   { 0, NULL }
2657 };
2658
2659
2660 static int
2661 dissect_snmp_SOutPDU_U(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2662   offset = dissect_ber_integer(implicit_tag, actx, tree, tvb, offset, hf_index,
2663                                                 NULL);
2664
2665   return offset;
2666 }
2667
2668
2669
2670 static int
2671 dissect_snmp_SOutPDU(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2672   offset = dissect_ber_tagged_type(implicit_tag, actx, tree, tvb, offset,
2673                                       hf_index, BER_CLASS_APP, 4, TRUE, dissect_snmp_SOutPDU_U);
2674
2675   return offset;
2676 }
2677
2678
2679 static const value_string snmp_SMUX_PDUs_vals[] = {
2680   {   0, "open" },
2681   {   1, "close" },
2682   {   2, "registerRequest" },
2683   {   3, "registerResponse" },
2684   {   4, "commitOrRollback" },
2685   { 0, NULL }
2686 };
2687
2688 static const ber_choice_t SMUX_PDUs_choice[] = {
2689   {   0, &hf_snmp_open           , BER_CLASS_ANY/*choice*/, -1/*choice*/, BER_FLAGS_NOOWNTAG, dissect_snmp_OpenPDU },
2690   {   1, &hf_snmp_close          , BER_CLASS_APP, 1, BER_FLAGS_NOOWNTAG, dissect_snmp_ClosePDU },
2691   {   2, &hf_snmp_registerRequest, BER_CLASS_APP, 2, BER_FLAGS_NOOWNTAG, dissect_snmp_RReqPDU },
2692   {   3, &hf_snmp_registerResponse, BER_CLASS_ANY/*choice*/, -1/*choice*/, BER_FLAGS_NOOWNTAG, dissect_snmp_RegisterResponse },
2693   {   4, &hf_snmp_commitOrRollback, BER_CLASS_APP, 4, BER_FLAGS_NOOWNTAG, dissect_snmp_SOutPDU },
2694   { 0, NULL, 0, 0, 0, NULL }
2695 };
2696
2697 static int
2698 dissect_snmp_SMUX_PDUs(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2699   offset = dissect_ber_choice(actx, tree, tvb, offset,
2700                                  SMUX_PDUs_choice, hf_index, ett_snmp_SMUX_PDUs,
2701                                  NULL);
2702
2703   return offset;
2704 }
2705
2706 /*--- PDUs ---*/
2707
2708 static void dissect_SMUX_PDUs_PDU(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_) {
2709   asn1_ctx_t asn1_ctx;
2710   asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, TRUE, pinfo);
2711   dissect_snmp_SMUX_PDUs(FALSE, tvb, 0, &asn1_ctx, tree, hf_snmp_SMUX_PDUs_PDU);
2712 }
2713
2714
2715 /*--- End of included file: packet-snmp-fn.c ---*/
2716 #line 1493 "packet-snmp-template.c"
2717
2718
2719 guint
2720 dissect_snmp_pdu(tvbuff_t *tvb, int offset, packet_info *pinfo,
2721                  proto_tree *tree, int proto, gint ett, gboolean is_tcp)
2722 {
2723
2724         guint length_remaining;
2725         gint8 class;
2726         gboolean pc, ind = 0;
2727         gint32 tag;
2728         guint32 len;
2729         guint message_length;
2730         int start_offset = offset;
2731         guint32 version = 0;
2732         tvbuff_t        *next_tvb;
2733
2734         proto_tree *snmp_tree = NULL;
2735         proto_item *item = NULL;
2736         asn1_ctx_t asn1_ctx;
2737         asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, TRUE, pinfo);
2738
2739
2740         usm_p.msg_tvb = tvb;
2741         usm_p.start_offset = tvb_offset_from_real_beginning(tvb);
2742         usm_p.engine_tvb = NULL;
2743         usm_p.user_tvb = NULL;
2744         usm_p.auth_item = NULL;
2745         usm_p.auth_tvb = NULL;
2746         usm_p.auth_offset = 0;
2747         usm_p.priv_tvb = NULL;
2748         usm_p.user_assoc = NULL;
2749         usm_p.authenticated = FALSE;
2750         usm_p.encrypted = FALSE;
2751         usm_p.boots = 0;
2752         usm_p.time = 0;
2753         usm_p.authOK = FALSE;
2754
2755         /*
2756          * This will throw an exception if we don't have any data left.
2757          * That's what we want.  (See "tcp_dissect_pdus()", which is
2758          * similar, but doesn't have to deal with ASN.1.
2759          * XXX - can we make "tcp_dissect_pdus()" provide enough
2760          * information to the "get_pdu_len" routine so that we could
2761          * have that routine deal with ASN.1, and just use
2762          * "tcp_dissect_pdus()"?)
2763          */
2764         length_remaining = tvb_ensure_length_remaining(tvb, offset);
2765
2766         /* NOTE: we have to parse the message piece by piece, since the
2767          * capture length may be less than the message length: a 'global'
2768          * parsing is likely to fail.
2769          */
2770
2771         /*
2772          * If this is SNMP-over-TCP, we might have to do reassembly
2773          * in order to read the "Sequence Of" header.
2774          */
2775         if (is_tcp && snmp_desegment && pinfo->can_desegment) {
2776                 /*
2777                  * This is TCP, and we should, and can, do reassembly.
2778                  *
2779                  * Is the "Sequence Of" header split across segment
2780                  * boundaries?  We requre at least 6 bytes for the
2781                  * header, which allows for a 4-byte length (ASN.1
2782                  * BER).
2783                  */
2784                 if (length_remaining < 6) {
2785                         pinfo->desegment_offset = offset;
2786                         pinfo->desegment_len = 6 - length_remaining;
2787
2788                         /*
2789                          * Return 0, which means "I didn't dissect anything
2790                          * because I don't have enough data - we need
2791                          * to desegment".
2792                          */
2793                         return 0;
2794                 }
2795         }
2796
2797         /*
2798          * OK, try to read the "Sequence Of" header; this gets the total
2799          * length of the SNMP message.
2800          */
2801         /* Set tree to 0 to not display internal BER fields if option used.*/
2802         offset = dissect_ber_identifier(pinfo, 0, tvb, offset, &class, &pc, &tag);
2803         /*Get the total octet length of the SNMP data*/
2804         offset = dissect_ber_length(pinfo, 0, tvb, offset, &len, &ind);
2805         message_length = len + 2;
2806
2807         /*Get the SNMP version data*/
2808         offset = dissect_ber_integer(FALSE, &asn1_ctx, 0, tvb, offset, -1, &version);
2809
2810
2811         /*
2812          * If this is SNMP-over-TCP, we might have to do reassembly
2813          * to get all of this message.
2814          */
2815         if (is_tcp && snmp_desegment && pinfo->can_desegment) {
2816                 /*
2817                  * Yes - is the message split across segment boundaries?
2818                  */
2819                 if (length_remaining < message_length) {
2820                         /*
2821                          * Yes.  Tell the TCP dissector where the data
2822                          * for this message starts in the data it handed
2823                          * us, and how many more bytes we need, and
2824                          * return.
2825                          */
2826                         pinfo->desegment_offset = start_offset;
2827                         pinfo->desegment_len =
2828                         message_length - length_remaining;
2829
2830                         /*
2831                          * Return 0, which means "I didn't dissect anything
2832                          * because I don't have enough data - we need
2833                          * to desegment".
2834                          */
2835                         return 0;
2836                 }
2837         }
2838
2839         next_tvb_init(&var_list);
2840
2841         col_set_str(pinfo->cinfo, COL_PROTOCOL,
2842             proto_get_protocol_short_name(find_protocol_by_id(proto)));
2843
2844         if (tree) {
2845                 item = proto_tree_add_item(tree, proto, tvb, start_offset,
2846                                            message_length, FALSE);
2847                 snmp_tree = proto_item_add_subtree(item, ett);
2848         }
2849
2850         switch (version) {
2851         case 0: /* v1 */
2852         case 1: /* v2c */
2853                 offset = dissect_snmp_Message(FALSE , tvb, start_offset, &asn1_ctx, snmp_tree, -1);
2854                 break;
2855         case 2: /* v2u */
2856                 offset = dissect_snmp_Messagev2u(FALSE , tvb, start_offset, &asn1_ctx, snmp_tree, -1);
2857                 break;
2858                         /* v3 */
2859         case 3:
2860                 offset = dissect_snmp_SNMPv3Message(FALSE , tvb, start_offset, &asn1_ctx, snmp_tree, -1);
2861                 break;
2862         default:
2863                 /*
2864                  * Return the length remaining in the tvbuff, so
2865                  * if this is SNMP-over-TCP, our caller thinks there's
2866                  * nothing left to dissect.
2867                  */
2868                 proto_tree_add_text(snmp_tree, tvb, offset, -1,"Unknown version");
2869                 return length_remaining;
2870                 break;
2871         }
2872
2873         /* There may be appended data after the SNMP data, so treat as raw
2874          * data which needs to be dissected in case of UDP as UDP is PDU oriented.
2875          */
2876         if((!is_tcp) && (length_remaining > (guint)offset)) {
2877                 next_tvb = tvb_new_subset_remaining(tvb, offset);
2878                 call_dissector(data_handle, next_tvb, pinfo, tree);
2879         } else {
2880                 next_tvb_call(&var_list, pinfo, tree, NULL, data_handle);
2881         }
2882
2883         return offset;
2884 }
2885
2886 static gint
2887 dissect_snmp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2888 {
2889         conversation_t  *conversation;
2890         int offset;
2891         gint8 tmp_class;
2892         gboolean tmp_pc;
2893         gint32 tmp_tag;
2894         guint32 tmp_length;
2895         gboolean tmp_ind;
2896
2897         /*
2898          * See if this looks like SNMP or not. if not, return 0 so
2899          * wireshark can try som other dissector instead.
2900          */
2901         /* All SNMP packets are BER encoded and consist of a SEQUENCE
2902          * that spans the entire PDU. The first item is an INTEGER that
2903          * has the values 0-2 (version 1-3).
2904          * if not it is not snmp.
2905          */
2906         /* SNMP starts with a SEQUENCE */
2907         offset = get_ber_identifier(tvb, 0, &tmp_class, &tmp_pc, &tmp_tag);
2908         if((tmp_class!=BER_CLASS_UNI)||(tmp_tag!=BER_UNI_TAG_SEQUENCE)) {
2909                 return 0;
2910         }
2911         /* then comes a length which spans the rest of the tvb */
2912         offset = get_ber_length(tvb, offset, &tmp_length, &tmp_ind);
2913         /* if(tmp_length!=(guint32)tvb_reported_length_remaining(tvb, offset)) {
2914          * Losen the heuristic a bit to handle the case where data has intentionally
2915          * been added after the snmp PDU ( UDP case)
2916          */
2917         if ( pinfo->ptype == PT_UDP ) {
2918                 if(tmp_length>(guint32)tvb_reported_length_remaining(tvb, offset)) {
2919                         return 0;
2920                 }
2921         }else{
2922                 if(tmp_length!=(guint32)tvb_reported_length_remaining(tvb, offset)) {
2923                         return 0;
2924                 }
2925         }
2926         /* then comes an INTEGER (version)*/
2927         offset = get_ber_identifier(tvb, offset, &tmp_class, &tmp_pc, &tmp_tag);
2928         if((tmp_class!=BER_CLASS_UNI)||(tmp_tag!=BER_UNI_TAG_INTEGER)) {
2929                 return 0;
2930         }
2931         /* do we need to test that version is 0 - 2 (version1-3) ? */
2932
2933
2934         /*
2935          * The first SNMP packet goes to the SNMP port; the second one
2936          * may come from some *other* port, but goes back to the same
2937          * IP address and port as the ones from which the first packet
2938          * came; all subsequent packets presumably go between those two
2939          * IP addresses and ports.
2940          *
2941          * If this packet went to the SNMP port, we check to see if
2942          * there's already a conversation with one address/port pair
2943          * matching the source IP address and port of this packet,
2944          * the other address matching the destination IP address of this
2945          * packet, and any destination port.
2946          *
2947          * If not, we create one, with its address 1/port 1 pair being
2948          * the source address/port of this packet, its address 2 being
2949          * the destination address of this packet, and its port 2 being
2950          * wildcarded, and give it the SNMP dissector as a dissector.
2951          */
2952         if (pinfo->destport == UDP_PORT_SNMP) {
2953           conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst, PT_UDP,
2954                                            pinfo->srcport, 0, NO_PORT_B);
2955           if( (conversation == NULL) || (conversation->dissector_handle!=snmp_handle) ) {
2956             conversation = conversation_new(pinfo->fd->num, &pinfo->src, &pinfo->dst, PT_UDP,
2957                                             pinfo->srcport, 0, NO_PORT2);
2958             conversation_set_dissector(conversation, snmp_handle);
2959           }
2960         }
2961
2962         return dissect_snmp_pdu(tvb, 0, pinfo, tree, proto_snmp, ett_snmp, FALSE);
2963 }
2964
2965 static void
2966 dissect_snmp_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2967 {
2968         int offset = 0;
2969         guint message_len;
2970
2971         while (tvb_reported_length_remaining(tvb, offset) > 0) {
2972                 message_len = dissect_snmp_pdu(tvb, 0, pinfo, tree,
2973                                                proto_snmp, ett_snmp, TRUE);
2974                 if (message_len == 0) {
2975                         /*
2976                          * We don't have all the data for that message,
2977                          * so we need to do desegmentation;
2978                          * "dissect_snmp_pdu()" has set that up.
2979                          */
2980                         break;
2981                 }
2982                 offset += message_len;
2983         }
2984 }
2985
2986 static void
2987 dissect_smux(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2988 {
2989         proto_tree *smux_tree = NULL;
2990         proto_item *item = NULL;
2991
2992         next_tvb_init(&var_list);
2993
2994         col_set_str(pinfo->cinfo, COL_PROTOCOL, "SMUX");
2995
2996         if (tree) {
2997                 item = proto_tree_add_item(tree, proto_smux, tvb, 0, -1, FALSE);
2998                 smux_tree = proto_item_add_subtree(item, ett_smux);
2999         }
3000
3001         dissect_SMUX_PDUs_PDU(tvb, pinfo, tree);
3002 }
3003
3004
3005 /*
3006   MD5 Password to Key Algorithm
3007   from RFC 3414 A.2.1
3008 */
3009 static void
3010 snmp_usm_password_to_key_md5(const guint8 *password, guint passwordlen,
3011                              const guint8 *engineID, guint   engineLength,
3012                              guint8 *key)
3013 {
3014         md5_state_t     MD;
3015         guint8     *cp, password_buf[64];
3016         guint32      password_index = 0;
3017         guint32      count = 0, i;
3018         guint8          key1[16];
3019         md5_init(&MD);   /* initialize MD5 */
3020
3021         /**********************************************/
3022         /* Use while loop until we've done 1 Megabyte */
3023         /**********************************************/
3024         while (count < 1048576) {
3025                 cp = password_buf;
3026                 for (i = 0; i < 64; i++) {
3027                         /*************************************************/
3028                         /* Take the next octet of the password, wrapping */
3029                         /* to the beginning of the password as necessary.*/
3030                         /*************************************************/
3031                         *cp++ = password[password_index++ % passwordlen];
3032                 }
3033                 md5_append(&MD, password_buf, 64);
3034                 count += 64;
3035         }
3036         md5_finish(&MD, key1);          /* tell MD5 we're done */
3037
3038         /*****************************************************/
3039         /* Now localize the key with the engineID and pass   */
3040         /* through MD5 to produce final key                  */
3041         /* May want to ensure that engineLength <= 32,       */
3042         /* otherwise need to use a buffer larger than 64     */
3043         /*****************************************************/
3044
3045         md5_init(&MD);
3046         md5_append(&MD, key1, 16);
3047         md5_append(&MD, engineID, engineLength);
3048         md5_append(&MD, key1, 16);
3049         md5_finish(&MD, key);
3050
3051         return;
3052 }
3053
3054
3055
3056
3057 /*
3058    SHA1 Password to Key Algorithm COPIED from RFC 3414 A.2.2
3059  */
3060
3061 static void
3062 snmp_usm_password_to_key_sha1(const guint8 *password, guint passwordlen,
3063                               const guint8 *engineID, guint engineLength,
3064                               guint8 *key)
3065 {
3066         sha1_context     SH;
3067         guint8     *cp, password_buf[72];
3068         guint32      password_index = 0;
3069         guint32      count = 0, i;
3070
3071         sha1_starts(&SH);   /* initialize SHA */
3072
3073         /**********************************************/
3074         /* Use while loop until we've done 1 Megabyte */
3075         /**********************************************/
3076         while (count < 1048576) {
3077                 cp = password_buf;
3078                 for (i = 0; i < 64; i++) {
3079                         /*************************************************/
3080                         /* Take the next octet of the password, wrapping */
3081                         /* to the beginning of the password as necessary.*/
3082                         /*************************************************/
3083                         *cp++ = password[password_index++ % passwordlen];
3084                 }
3085                 sha1_update (&SH, password_buf, 64);
3086                 count += 64;
3087         }
3088         sha1_finish(&SH, key);
3089
3090         /*****************************************************/
3091         /* Now localize the key with the engineID and pass   */
3092         /* through SHA to produce final key                  */
3093         /* May want to ensure that engineLength <= 32,       */
3094         /* otherwise need to use a buffer larger than 72     */
3095         /*****************************************************/
3096         memcpy(password_buf, key, 20);
3097         memcpy(password_buf+20, engineID, engineLength);
3098         memcpy(password_buf+20+engineLength, key, 20);
3099
3100         sha1_starts(&SH);
3101         sha1_update(&SH, password_buf, 40+engineLength);
3102         sha1_finish(&SH, key);
3103         return;
3104  }
3105
3106
3107 static void
3108 process_prefs(void)
3109 {
3110 }
3111
3112 static void*
3113 snmp_users_copy_cb(void* dest, const void* orig, unsigned len _U_)
3114 {
3115         const snmp_ue_assoc_t* o = orig;
3116         snmp_ue_assoc_t* d = dest;
3117
3118         d->auth_model = o->auth_model;
3119         d->user.authModel = auth_models[o->auth_model];
3120
3121         d->priv_proto = o->priv_proto;
3122         d->user.privProtocol = priv_protos[o->priv_proto];
3123
3124         d->user.userName.data = g_memdup(o->user.userName.data,o->user.userName.len);
3125         d->user.userName.len = o->user.userName.len;
3126
3127         d->user.authPassword.data = o->user.authPassword.data ? g_memdup(o->user.authPassword.data,o->user.authPassword.len) : NULL;
3128         d->user.authPassword.len = o->user.authPassword.len;
3129
3130         d->user.privPassword.data = o->user.privPassword.data ? g_memdup(o->user.privPassword.data,o->user.privPassword.len) : NULL;
3131         d->user.privPassword.len = o->user.privPassword.len;
3132
3133         d->engine.len = o->engine.len;
3134         if (o->engine.data) {
3135                 d->engine.data = g_memdup(o->engine.data,o->engine.len);
3136         }
3137
3138         d->user.authKey.data = o->user.authKey.data ? g_memdup(o->user.authKey.data,o->user.authKey.len) : NULL;
3139         d->user.authKey.len = o->user.authKey.len;
3140
3141         d->user.privKey.data = o->user.privKey.data ? g_memdup(o->user.privKey.data,o->user.privKey.len) : NULL;
3142         d->user.privKey.len = o->user.privKey.len;
3143
3144         return d;
3145 }
3146
3147 static void
3148 snmp_users_free_cb(void* p)
3149 {
3150         snmp_ue_assoc_t* ue = p;
3151         g_free(ue->user.userName.data);
3152         g_free(ue->user.authPassword.data);
3153         g_free(ue->user.privPassword.data);
3154         g_free(ue->user.authKey.data);
3155         g_free(ue->user.privKey.data);
3156         g_free(ue->engine.data);
3157 }
3158
3159 static void
3160 snmp_users_update_cb(void* p _U_, const char** err)
3161 {
3162         snmp_ue_assoc_t* ue = p;
3163         GString* es = g_string_new("");
3164         unsigned i;
3165
3166         *err = NULL;
3167
3168         if (num_ueas == 0)
3169                 /* Nothing to update */
3170                 return;
3171
3172         if (! ue->user.userName.len)
3173                 g_string_append_printf(es,"no userName\n");
3174
3175         for (i=0; i<num_ueas-1; i++) {
3176                 snmp_ue_assoc_t* u = &(ueas[i]);
3177
3178
3179                 if ( u->user.userName.len == ue->user.userName.len
3180                         && u->engine.len == ue->engine.len ) {
3181
3182                         if (u->engine.len > 0 && memcmp( u->engine.data,   ue->engine.data,  u->engine.len ) == 0) {
3183                                 if ( memcmp( u->user.userName.data, ue->user.userName.data, ue->user.userName.len ) == 0 ) {
3184                                         /* XXX: make a string for the engineId */
3185                                         g_string_append_printf(es,"duplicate key (userName='%s')\n",ue->user.userName.data);
3186                                 }
3187                         }
3188
3189                         if (u->engine.len == 0) {
3190                                 if ( memcmp( u->user.userName.data, ue->user.userName.data, ue->user.userName.len ) == 0 ) {
3191                                         g_string_append_printf(es,"duplicate key (userName='%s' engineId=NONE)\n",ue->user.userName.data);
3192                                 }
3193                         }
3194                 }
3195         }
3196
3197         if (es->len) {
3198                 g_string_truncate(es,es->len-2);
3199                 *err = ep_strdup(es->str);
3200         }
3201
3202         g_string_free(es,TRUE);
3203
3204         return;
3205 }
3206
3207
3208 UAT_LSTRING_CB_DEF(snmp_users,userName,snmp_ue_assoc_t,user.userName.data,user.userName.len)
3209 UAT_LSTRING_CB_DEF(snmp_users,authPassword,snmp_ue_assoc_t,user.authPassword.data,user.authPassword.len)
3210 UAT_LSTRING_CB_DEF(snmp_users,privPassword,snmp_ue_assoc_t,user.privPassword.data,user.privPassword.len)
3211 UAT_BUFFER_CB_DEF(snmp_users,engine_id,snmp_ue_assoc_t,engine.data,engine.len)
3212 UAT_VS_DEF(snmp_users,auth_model,snmp_ue_assoc_t,0,"MD5")
3213 UAT_VS_DEF(snmp_users,priv_proto,snmp_ue_assoc_t,0,"DES")
3214
3215 static void *
3216 snmp_specific_trap_copy_cb(void *dest, const void *orig, unsigned len _U_)
3217 {
3218         snmp_st_assoc_t *u = dest;
3219         const snmp_st_assoc_t *o = orig;
3220
3221         u->enterprise = g_strdup(o->enterprise);
3222         u->trap = o->trap;
3223         u->desc = g_strdup(o->desc);
3224
3225         return dest;
3226 }
3227
3228 static void
3229 snmp_specific_trap_free_cb(void *r)
3230 {
3231         snmp_st_assoc_t *u = r;
3232
3233         g_free(u->enterprise);
3234         g_free(u->desc);
3235 }
3236
3237 UAT_CSTRING_CB_DEF(specific_traps, enterprise, snmp_st_assoc_t)
3238 UAT_DEC_CB_DEF(specific_traps, trap, snmp_st_assoc_t)
3239 UAT_CSTRING_CB_DEF(specific_traps, desc, snmp_st_assoc_t)
3240
3241         /*--- proto_register_snmp -------------------------------------------*/
3242 void proto_register_snmp(void) {
3243   /* List of fields */
3244   static hf_register_info hf[] = {
3245                 { &hf_snmp_v3_flags_auth,
3246                 { "Authenticated", "snmp.v3.flags.auth", FT_BOOLEAN, 8,
3247                     TFS(&tfs_set_notset), TH_AUTH, NULL, HFILL }},
3248                 { &hf_snmp_v3_flags_crypt,
3249                 { "Encrypted", "snmp.v3.flags.crypt", FT_BOOLEAN, 8,
3250                     TFS(&tfs_set_notset), TH_CRYPT, NULL, HFILL }},
3251                 { &hf_snmp_v3_flags_report,
3252                 { "Reportable", "snmp.v3.flags.report", FT_BOOLEAN, 8,
3253                     TFS(&tfs_set_notset), TH_REPORT, NULL, HFILL }},
3254                 { &hf_snmp_engineid_conform, {
3255                     "Engine ID Conformance", "snmp.engineid.conform", FT_BOOLEAN, 8,
3256                     TFS(&tfs_snmp_engineid_conform), F_SNMP_ENGINEID_CONFORM, "Engine ID RFC3411 Conformance", HFILL }},
3257                 { &hf_snmp_engineid_enterprise, {
3258                     "Engine Enterprise ID", "snmp.engineid.enterprise", FT_UINT32, BASE_DEC|BASE_EXT_STRING,
3259                     &sminmpec_values_ext, 0, NULL, HFILL }},
3260                 { &hf_snmp_engineid_format, {
3261                     "Engine ID Format", "snmp.engineid.format", FT_UINT8, BASE_DEC,
3262                     VALS(snmp_engineid_format_vals), 0, NULL, HFILL }},
3263                 { &hf_snmp_engineid_ipv4, {
3264                     "Engine ID Data: IPv4 address", "snmp.engineid.ipv4", FT_IPv4, BASE_NONE,
3265                     NULL, 0, NULL, HFILL }},
3266                 { &hf_snmp_engineid_ipv6, {
3267                     "Engine ID Data: IPv6 address", "snmp.engineid.ipv6", FT_IPv6, BASE_NONE,
3268                     NULL, 0, NULL, HFILL }},
3269                 { &hf_snmp_engineid_cisco_type, {
3270                     "Engine ID Data: Cisco type", "snmp.engineid.cisco.type", FT_UINT8, BASE_NONE,
3271                     VALS(snmp_engineid_cisco_type_vals), 0, NULL, HFILL }},
3272                 { &hf_snmp_engineid_mac, {
3273                     "Engine ID Data: MAC address", "snmp.engineid.mac", FT_ETHER, BASE_NONE,
3274                     NULL, 0, NULL, HFILL }},
3275                 { &hf_snmp_engineid_text, {
3276                     "Engine ID Data: Text", "snmp.engineid.text", FT_STRING, BASE_NONE,
3277                     NULL, 0, NULL, HFILL }},
3278                 { &hf_snmp_engineid_time, {
3279                     "Engine ID Data: Creation Time", "snmp.engineid.time", FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL,
3280                     NULL, 0, NULL, HFILL }},
3281                 { &hf_snmp_engineid_data, {
3282                     "Engine ID Data", "snmp.engineid.data", FT_BYTES, BASE_NONE,
3283                     NULL, 0, NULL, HFILL }},
3284                 { &hf_snmp_msgAuthentication, {
3285                     "Authentication", "snmp.v3.auth", FT_BOOLEAN, BASE_NONE,
3286                     TFS(&auth_flags), 0, NULL, HFILL }},
3287                 { &hf_snmp_decryptedPDU, {
3288                     "Decrypted ScopedPDU", "snmp.decrypted_pdu", FT_BYTES, BASE_NONE,
3289                     NULL, 0, "Decrypted PDU", HFILL }},
3290                 { &hf_snmp_noSuchObject, {
3291                     "noSuchObject", "snmp.noSuchObject", FT_NONE, BASE_NONE,
3292                     NULL, 0, NULL, HFILL }},
3293                 { &hf_snmp_noSuchInstance, {
3294                     "noSuchInstance", "snmp.noSuchInstance", FT_NONE, BASE_NONE,
3295                     NULL, 0, NULL, HFILL }},
3296                 { &hf_snmp_endOfMibView, {
3297                     "endOfMibView", "snmp.endOfMibView", FT_NONE, BASE_NONE,
3298                     NULL, 0, NULL, HFILL }},
3299                 { &hf_snmp_unSpecified, {
3300                     "unSpecified", "snmp.unSpecified", FT_NONE, BASE_NONE,
3301                     NULL, 0, NULL, HFILL }},
3302
3303                 { &hf_snmp_integer32_value, {
3304                     "Value (Integer32)", "snmp.value.int", FT_INT64, BASE_DEC,
3305                     NULL, 0, NULL, HFILL }},
3306                 { &hf_snmp_octetstring_value, {
3307                     "Value (OctetString)", "snmp.value.octets", FT_BYTES, BASE_NONE,
3308                     NULL, 0, NULL, HFILL }},
3309                 { &hf_snmp_oid_value, {
3310                     "Value (OID)", "snmp.value.oid", FT_OID, BASE_NONE,
3311                     NULL, 0, NULL, HFILL }},
3312                 { &hf_snmp_null_value, {
3313                     "Value (Null)", "snmp.value.null", FT_NONE, BASE_NONE,
3314                     NULL, 0, NULL, HFILL }},
3315                 { &hf_snmp_ipv4_value, {
3316                     "Value (IpAddress)", "snmp.value.ipv4", FT_IPv4, BASE_NONE,
3317                     NULL, 0, NULL, HFILL }},
3318                 { &hf_snmp_ipv6_value, {
3319                     "Value (IpAddress)", "snmp.value.ipv6", FT_IPv6, BASE_NONE,
3320                     NULL, 0, NULL, HFILL }},
3321                 { &hf_snmp_anyaddress_value, {
3322                     "Value (IpAddress)", "snmp.value.addr", FT_BYTES, BASE_NONE,
3323                     NULL, 0, NULL, HFILL }},
3324                 { &hf_snmp_unsigned32_value, {
3325                     "Value (Unsigned32)", "snmp.value.u32", FT_INT64, BASE_DEC,
3326                     NULL, 0, NULL, HFILL }},
3327                 { &hf_snmp_gauge32_value, {
3328                     "Value (Gauge32)", "snmp.value.g32", FT_INT64, BASE_DEC,
3329                     NULL, 0, NULL, HFILL }},
3330                 { &hf_snmp_unknown_value, {
3331                     "Value (Unknown)", "snmp.value.unk", FT_BYTES, BASE_NONE,
3332                     NULL, 0, NULL, HFILL }},
3333                 { &hf_snmp_counter_value, {
3334                     "Value (Counter32)", "snmp.value.counter", FT_UINT64, BASE_DEC,
3335                     NULL, 0, NULL, HFILL }},
3336                 { &hf_snmp_big_counter_value, {
3337                     "Value (Counter64)", "snmp.value.counter", FT_UINT64, BASE_DEC,
3338                     NULL, 0, NULL, HFILL }},
3339                 { &hf_snmp_nsap_value, {
3340                     "Value (NSAP)", "snmp.value.nsap", FT_UINT64, BASE_DEC,
3341                     NULL, 0, NULL, HFILL }},
3342                 { &hf_snmp_timeticks_value, {
3343                     "Value (Timeticks)", "snmp.value.timeticks", FT_UINT64, BASE_DEC,
3344                     NULL, 0, NULL, HFILL }},
3345                 { &hf_snmp_opaque_value, {
3346                     "Value (Opaque)", "snmp.value.opaque", FT_BYTES, BASE_NONE,
3347                     NULL, 0, NULL, HFILL }},
3348                 { &hf_snmp_objectname, {
3349                     "Object Name", "snmp.name", FT_OID, BASE_NONE,
3350                     NULL, 0, NULL, HFILL }},
3351                 { &hf_snmp_scalar_instance_index, {
3352                     "Scalar Instance Index", "snmp.name.index", FT_UINT64, BASE_DEC,
3353                     NULL, 0, NULL, HFILL }},
3354
3355
3356
3357 /*--- Included file: packet-snmp-hfarr.c ---*/
3358 #line 1 "packet-snmp-hfarr.c"
3359     { &hf_snmp_SMUX_PDUs_PDU,
3360       { "SMUX-PDUs", "snmp.SMUX_PDUs",
3361         FT_UINT32, BASE_DEC, VALS(snmp_SMUX_PDUs_vals), 0,
3362         NULL, HFILL }},
3363     { &hf_snmp_version,
3364       { "version", "snmp.version",
3365         FT_INT32, BASE_DEC, VALS(snmp_Version_vals), 0,
3366         NULL, HFILL }},
3367     { &hf_snmp_community,
3368       { "community", "snmp.community",
3369         FT_STRING, BASE_NONE, NULL, 0,
3370         NULL, HFILL }},
3371     { &hf_snmp_data,
3372       { "data", "snmp.data",
3373         FT_UINT32, BASE_DEC, VALS(snmp_PDUs_vals), 0,
3374         "PDUs", HFILL }},
3375     { &hf_snmp_parameters,
3376       { "parameters", "snmp.parameters",
3377         FT_BYTES, BASE_NONE, NULL, 0,
3378         "OCTET_STRING", HFILL }},
3379     { &hf_snmp_datav2u,
3380       { "datav2u", "snmp.datav2u",
3381         FT_UINT32, BASE_DEC, VALS(snmp_T_datav2u_vals), 0,
3382         NULL, HFILL }},
3383     { &hf_snmp_v2u_plaintext,
3384       { "plaintext", "snmp.plaintext",
3385         FT_UINT32, BASE_DEC, VALS(snmp_PDUs_vals), 0,
3386         "PDUs", HFILL }},
3387     { &hf_snmp_encrypted,
3388       { "encrypted", "snmp.encrypted",
3389         FT_BYTES, BASE_NONE, NULL, 0,
3390         "OCTET_STRING", HFILL }},
3391     { &hf_snmp_msgAuthoritativeEngineID,
3392       { "msgAuthoritativeEngineID", "snmp.msgAuthoritativeEngineID",
3393         FT_BYTES, BASE_NONE, NULL, 0,
3394         NULL, HFILL }},
3395     { &hf_snmp_msgAuthoritativeEngineBoots,
3396       { "msgAuthoritativeEngineBoots", "snmp.msgAuthoritativeEngineBoots",
3397         FT_UINT32, BASE_DEC, NULL, 0,
3398         NULL, HFILL }},
3399     { &hf_snmp_msgAuthoritativeEngineTime,
3400       { "msgAuthoritativeEngineTime", "snmp.msgAuthoritativeEngineTime",
3401         FT_UINT32, BASE_DEC, NULL, 0,
3402         NULL, HFILL }},
3403     { &hf_snmp_msgUserName,
3404       { "msgUserName", "snmp.msgUserName",
3405         FT_STRING, BASE_NONE, NULL, 0,
3406         NULL, HFILL }},
3407     { &hf_snmp_msgAuthenticationParameters,
3408       { "msgAuthenticationParameters", "snmp.msgAuthenticationParameters",
3409         FT_BYTES, BASE_NONE, NULL, 0,
3410         NULL, HFILL }},
3411     { &hf_snmp_msgPrivacyParameters,
3412       { "msgPrivacyParameters", "snmp.msgPrivacyParameters",
3413         FT_BYTES, BASE_NONE, NULL, 0,
3414         NULL, HFILL }},
3415     { &hf_snmp_msgVersion,
3416       { "msgVersion", "snmp.msgVersion",
3417         FT_INT32, BASE_DEC, VALS(snmp_Version_vals), 0,
3418         "Version", HFILL }},
3419     { &hf_snmp_msgGlobalData,
3420       { "msgGlobalData", "snmp.msgGlobalData",
3421         FT_NONE, BASE_NONE, NULL, 0,
3422         "HeaderData", HFILL }},
3423     { &hf_snmp_msgSecurityParameters,
3424       { "msgSecurityParameters", "snmp.msgSecurityParameters",
3425         FT_BYTES, BASE_NONE, NULL, 0,
3426         NULL, HFILL }},
3427     { &hf_snmp_msgData,
3428       { "msgData", "snmp.msgData",
3429         FT_UINT32, BASE_DEC, VALS(snmp_ScopedPduData_vals), 0,
3430         "ScopedPduData", HFILL }},
3431     { &hf_snmp_msgID,
3432       { "msgID", "snmp.msgID",
3433         FT_UINT32, BASE_DEC, NULL, 0,
3434         "INTEGER_0_2147483647", HFILL }},
3435     { &hf_snmp_msgMaxSize,
3436       { "msgMaxSize", "snmp.msgMaxSize",
3437         FT_UINT32, BASE_DEC, NULL, 0,
3438         "INTEGER_484_2147483647", HFILL }},
3439     { &hf_snmp_msgFlags,
3440       { "msgFlags", "snmp.msgFlags",
3441         FT_BYTES, BASE_NONE, NULL, 0,
3442         NULL, HFILL }},
3443     { &hf_snmp_msgSecurityModel,
3444       { "msgSecurityModel", "snmp.msgSecurityModel",
3445         FT_UINT32, BASE_DEC, VALS(sec_models), 0,
3446         NULL, HFILL }},
3447     { &hf_snmp_plaintext,
3448       { "plaintext", "snmp.plaintext",
3449         FT_NONE, BASE_NONE, NULL, 0,
3450         "ScopedPDU", HFILL }},
3451     { &hf_snmp_encryptedPDU,
3452       { "encryptedPDU", "snmp.encryptedPDU",
3453         FT_BYTES, BASE_NONE, NULL, 0,
3454         NULL, HFILL }},
3455     { &hf_snmp_contextEngineID,
3456       { "contextEngineID", "snmp.contextEngineID",
3457         FT_BYTES, BASE_NONE, NULL, 0,
3458         "SnmpEngineID", HFILL }},
3459     { &hf_snmp_contextName,
3460       { "contextName", "snmp.contextName",
3461         FT_BYTES, BASE_NONE, NULL, 0,
3462         "OCTET_STRING", HFILL }},
3463     { &hf_snmp_get_request,
3464       { "get-request", "snmp.get_request",
3465         FT_NONE, BASE_NONE, NULL, 0,
3466         "GetRequest_PDU", HFILL }},
3467     { &hf_snmp_get_next_request,
3468       { "get-next-request", "snmp.get_next_request",
3469         FT_NONE, BASE_NONE, NULL, 0,
3470         "GetNextRequest_PDU", HFILL }},
3471     { &hf_snmp_get_response,
3472       { "get-response", "snmp.get_response",
3473         FT_NONE, BASE_NONE, NULL, 0,
3474         "GetResponse_PDU", HFILL }},
3475     { &hf_snmp_set_request,
3476       { "set-request", "snmp.set_request",
3477         FT_NONE, BASE_NONE, NULL, 0,
3478         "SetRequest_PDU", HFILL }},
3479     { &hf_snmp_trap,
3480       { "trap", "snmp.trap",
3481         FT_NONE, BASE_NONE, NULL, 0,
3482         "Trap_PDU", HFILL }},
3483     { &hf_snmp_getBulkRequest,
3484       { "getBulkRequest", "snmp.getBulkRequest",
3485         FT_NONE, BASE_NONE, NULL, 0,
3486         "GetBulkRequest_PDU", HFILL }},
3487     { &hf_snmp_informRequest,
3488       { "informRequest", "snmp.informRequest",
3489         FT_NONE, BASE_NONE, NULL, 0,
3490         "InformRequest_PDU", HFILL }},
3491     { &hf_snmp_sNMPv2_Trap,
3492       { "sNMPv2-Trap", "snmp.sNMPv2_Trap",
3493         FT_NONE, BASE_NONE, NULL, 0,
3494         "SNMPv2_Trap_PDU", HFILL }},
3495     { &hf_snmp_report,
3496       { "report", "snmp.report",
3497         FT_NONE, BASE_NONE, NULL, 0,
3498         "Report_PDU", HFILL }},
3499     { &hf_snmp_request_id,
3500       { "request-id", "snmp.request_id",
3501         FT_INT32, BASE_DEC, NULL, 0,
3502         "INTEGER", HFILL }},
3503     { &hf_snmp_error_status,
3504       { "error-status", "snmp.error_status",
3505         FT_INT32, BASE_DEC, VALS(snmp_T_error_status_vals), 0,
3506         NULL, HFILL }},
3507     { &hf_snmp_error_index,
3508       { "error-index", "snmp.error_index",
3509         FT_INT32, BASE_DEC, NULL, 0,
3510         "INTEGER", HFILL }},
3511     { &hf_snmp_variable_bindings,
3512       { "variable-bindings", "snmp.variable_bindings",
3513         FT_UINT32, BASE_DEC, NULL, 0,
3514         "VarBindList", HFILL }},
3515     { &hf_snmp_bulkPDU_request_id,
3516       { "request-id", "snmp.request_id",
3517         FT_INT32, BASE_DEC, NULL, 0,
3518         "Integer32", HFILL }},
3519     { &hf_snmp_non_repeaters,
3520       { "non-repeaters", "snmp.non_repeaters",
3521         FT_UINT32, BASE_DEC, NULL, 0,
3522         "INTEGER_0_2147483647", HFILL }},
3523     { &hf_snmp_max_repetitions,
3524       { "max-repetitions", "snmp.max_repetitions",
3525         FT_UINT32, BASE_DEC, NULL, 0,
3526         "INTEGER_0_2147483647", HFILL }},
3527     { &hf_snmp_enterprise,
3528       { "enterprise", "snmp.enterprise",
3529         FT_OID, BASE_NONE, NULL, 0,
3530         "EnterpriseOID", HFILL }},
3531     { &hf_snmp_agent_addr,
3532       { "agent-addr", "snmp.agent_addr",
3533         FT_IPv4, BASE_NONE, NULL, 0,
3534         "NetworkAddress", HFILL }},
3535     { &hf_snmp_generic_trap,
3536       { "generic-trap", "snmp.generic_trap",
3537         FT_INT32, BASE_DEC, VALS(snmp_GenericTrap_vals), 0,
3538         "GenericTrap", HFILL }},
3539     { &hf_snmp_specific_trap,
3540       { "specific-trap", "snmp.specific_trap",
3541         FT_INT32, BASE_DEC, NULL, 0,
3542         "SpecificTrap", HFILL }},
3543     { &hf_snmp_time_stamp,
3544       { "time-stamp", "snmp.time_stamp",
3545         FT_UINT32, BASE_DEC, NULL, 0,
3546         "TimeTicks", HFILL }},
3547     { &hf_snmp_name,
3548       { "name", "snmp.name",
3549         FT_OID, BASE_NONE, NULL, 0,
3550         "ObjectName", HFILL }},
3551     { &hf_snmp_valueType,
3552       { "valueType", "snmp.valueType",
3553         FT_NONE, BASE_NONE, NULL, 0,
3554         NULL, HFILL }},
3555     { &hf_snmp_VarBindList_item,
3556       { "VarBind", "snmp.VarBind",
3557         FT_NONE, BASE_NONE, NULL, 0,
3558         NULL, HFILL }},
3559     { &hf_snmp_open,
3560       { "open", "snmp.open",
3561         FT_UINT32, BASE_DEC, VALS(snmp_OpenPDU_vals), 0,
3562         "OpenPDU", HFILL }},
3563     { &hf_snmp_close,
3564       { "close", "snmp.close",
3565         FT_INT32, BASE_DEC, VALS(snmp_ClosePDU_U_vals), 0,
3566         "ClosePDU", HFILL }},
3567     { &hf_snmp_registerRequest,
3568       { "registerRequest", "snmp.registerRequest",
3569         FT_NONE, BASE_NONE, NULL, 0,
3570         "RReqPDU", HFILL }},
3571     { &hf_snmp_registerResponse,
3572       { "registerResponse", "snmp.registerResponse",
3573         FT_UINT32, BASE_DEC, VALS(snmp_RegisterResponse_vals), 0,
3574         NULL, HFILL }},
3575     { &hf_snmp_commitOrRollback,
3576       { "commitOrRollback", "snmp.commitOrRollback",
3577         FT_INT32, BASE_DEC, VALS(snmp_SOutPDU_U_vals), 0,
3578         "SOutPDU", HFILL }},
3579     { &hf_snmp_rRspPDU,
3580       { "rRspPDU", "snmp.rRspPDU",
3581         FT_INT32, BASE_DEC, VALS(snmp_RRspPDU_U_vals), 0,
3582         NULL, HFILL }},
3583     { &hf_snmp_pDUs,
3584       { "pDUs", "snmp.pDUs",
3585         FT_UINT32, BASE_DEC, VALS(snmp_PDUs_vals), 0,
3586         NULL, HFILL }},
3587     { &hf_snmp_smux_simple,
3588       { "smux-simple", "snmp.smux_simple",
3589         FT_NONE, BASE_NONE, NULL, 0,
3590         "SimpleOpen", HFILL }},
3591     { &hf_snmp_smux_version,
3592       { "smux-version", "snmp.smux_version",
3593         FT_INT32, BASE_DEC, VALS(snmp_T_smux_version_vals), 0,
3594         NULL, HFILL }},
3595     { &hf_snmp_identity,
3596       { "identity", "snmp.identity",
3597         FT_OID, BASE_NONE, NULL, 0,
3598         "OBJECT_IDENTIFIER", HFILL }},
3599     { &hf_snmp_description,
3600       { "description", "snmp.description",
3601         FT_BYTES, BASE_NONE, NULL, 0,
3602         "DisplayString", HFILL }},
3603     { &hf_snmp_password,
3604       { "password", "snmp.password",
3605         FT_BYTES, BASE_NONE, NULL, 0,
3606         "OCTET_STRING", HFILL }},
3607     { &hf_snmp_subtree,
3608       { "subtree", "snmp.subtree",
3609         FT_OID, BASE_NONE, NULL, 0,
3610         "ObjectName", HFILL }},
3611     { &hf_snmp_priority,
3612       { "priority", "snmp.priority",
3613         FT_INT32, BASE_DEC, NULL, 0,
3614         "INTEGER_M1_2147483647", HFILL }},
3615     { &hf_snmp_operation,
3616       { "operation", "snmp.operation",
3617         FT_INT32, BASE_DEC, VALS(snmp_T_operation_vals), 0,
3618         NULL, HFILL }},
3619
3620 /*--- End of included file: packet-snmp-hfarr.c ---*/
3621 #line 2133 "packet-snmp-template.c"
3622   };
3623
3624   /* List of subtrees */
3625   static gint *ett[] = {
3626           &ett_snmp,
3627           &ett_engineid,
3628           &ett_msgFlags,
3629           &ett_encryptedPDU,
3630           &ett_decrypted,
3631           &ett_authParameters,
3632           &ett_internet,
3633           &ett_varbind,
3634           &ett_name,
3635           &ett_value,
3636           &ett_decoding_error,
3637
3638 /*--- Included file: packet-snmp-ettarr.c ---*/
3639 #line 1 "packet-snmp-ettarr.c"
3640     &ett_snmp_Message,
3641     &ett_snmp_Messagev2u,
3642     &ett_snmp_T_datav2u,
3643     &ett_snmp_UsmSecurityParameters,
3644     &ett_snmp_SNMPv3Message,
3645     &ett_snmp_HeaderData,
3646     &ett_snmp_ScopedPduData,
3647     &ett_snmp_ScopedPDU,
3648     &ett_snmp_PDUs,
3649     &ett_snmp_PDU,
3650     &ett_snmp_BulkPDU,
3651     &ett_snmp_Trap_PDU_U,
3652     &ett_snmp_VarBind,
3653     &ett_snmp_VarBindList,
3654     &ett_snmp_SMUX_PDUs,
3655     &ett_snmp_RegisterResponse,
3656     &ett_snmp_OpenPDU,
3657     &ett_snmp_SimpleOpen_U,
3658     &ett_snmp_RReqPDU_U,
3659
3660 /*--- End of included file: packet-snmp-ettarr.c ---*/
3661 #line 2149 "packet-snmp-template.c"
3662   };
3663   module_t *snmp_module;
3664
3665   static uat_field_t users_fields[] = {
3666           UAT_FLD_BUFFER(snmp_users,engine_id,"Engine ID","Engine-id for this entry (empty = any)"),
3667           UAT_FLD_LSTRING(snmp_users,userName,"Username","The username"),
3668           UAT_FLD_VS(snmp_users,auth_model,"Authentication model",auth_types,"Algorithm to be used for authentication."),
3669           UAT_FLD_LSTRING(snmp_users,authPassword,"Password","The password used for authenticating packets for this entry"),
3670           UAT_FLD_VS(snmp_users,priv_proto,"Privacy protocol",priv_types,"Algorithm to be used for privacy."),
3671           UAT_FLD_LSTRING(snmp_users,privPassword,"Privacy password","The password used for encrypting packets for this entry"),
3672           UAT_END_FIELDS
3673   };
3674
3675   uat_t *assocs_uat = uat_new("SNMP Users",
3676                               sizeof(snmp_ue_assoc_t),
3677                               "snmp_users",
3678                               TRUE,
3679                               (void*)&ueas,
3680                               &num_ueas,
3681                               UAT_CAT_CRYPTO,
3682                               "ChSNMPUsersSection",
3683                               snmp_users_copy_cb,
3684                               snmp_users_update_cb,
3685                               snmp_users_free_cb,
3686                               renew_ue_cache,
3687                               users_fields);
3688
3689   static uat_field_t specific_traps_flds[] = {
3690     UAT_FLD_CSTRING(specific_traps,enterprise,"Enterprise OID","Enterprise Object Identifier"),
3691     UAT_FLD_DEC(specific_traps,trap,"Trap Id","The specific-trap value"),
3692     UAT_FLD_CSTRING(specific_traps,desc,"Description","Trap type description"),
3693     UAT_END_FIELDS
3694   };
3695
3696   uat_t* specific_traps_uat = uat_new("SNMP Enterprise Specific Trap Types",
3697                                       sizeof(snmp_st_assoc_t),
3698                                       "snmp_specific_traps",
3699                                       TRUE,
3700                                       (void*) &specific_traps,
3701                                       &num_specific_traps,
3702                                       UAT_CAT_GENERAL,
3703                                       "ChSNMPEnterpriseSpecificTrapTypes",
3704                                       snmp_specific_trap_copy_cb,
3705                                       NULL,
3706                                       snmp_specific_trap_free_cb,
3707                                                                           NULL,
3708                                       specific_traps_flds);
3709
3710   /* Register protocol */
3711   proto_snmp = proto_register_protocol(PNAME, PSNAME, PFNAME);
3712   new_register_dissector("snmp", dissect_snmp, proto_snmp);
3713
3714   /* Register fields and subtrees */
3715   proto_register_field_array(proto_snmp, hf, array_length(hf));
3716   proto_register_subtree_array(ett, array_length(ett));
3717
3718
3719   /* Register configuration preferences */
3720   snmp_module = prefs_register_protocol(proto_snmp, process_prefs);
3721   prefs_register_bool_preference(snmp_module, "display_oid",
3722                         "Show SNMP OID in info column",
3723                         "Whether the SNMP OID should be shown in the info column",
3724                         &display_oid);
3725
3726   prefs_register_obsolete_preference(snmp_module, "mib_modules");
3727   prefs_register_obsolete_preference(snmp_module, "users_file");
3728
3729   prefs_register_bool_preference(snmp_module, "desegment",
3730                         "Reassemble SNMP-over-TCP messages\nspanning multiple TCP segments",
3731                         "Whether the SNMP dissector should reassemble messages spanning multiple TCP segments."
3732                         " To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
3733                         &snmp_desegment);
3734
3735   prefs_register_bool_preference(snmp_module, "var_in_tree",
3736                         "Display dissected variables inside SNMP tree",
3737                         "ON - display dissected variables inside SNMP tree, OFF - display dissected variables in root tree after SNMP",
3738                         &snmp_var_in_tree);
3739
3740   prefs_register_uat_preference(snmp_module, "users_table",
3741                                 "Users Table",
3742                                 "Table of engine-user associations used for authentication and decryption",
3743                                 assocs_uat);
3744
3745   prefs_register_uat_preference(snmp_module, "specific_traps_table",
3746                                 "Enterprise Specific Trap Types",
3747                                 "Table of enterprise specific-trap type descriptions",
3748                                 specific_traps_uat);
3749
3750 #ifdef HAVE_LIBSMI
3751   prefs_register_static_text_preference(snmp_module, "info_mibs",
3752                                         "MIB settings can be changed in the Name Resolution preferences",
3753                                         "MIB settings can be changed in the Name Resolution preferences");
3754 #endif
3755
3756   value_sub_dissectors_table = register_dissector_table("snmp.variable_oid","SNMP Variable OID", FT_STRING, BASE_NONE);
3757
3758   register_init_routine(renew_ue_cache);
3759
3760   register_ber_syntax_dissector("SNMP", proto_snmp, dissect_snmp_tcp);
3761 }
3762
3763
3764 /*--- proto_reg_handoff_snmp ---------------------------------------*/
3765 void proto_reg_handoff_snmp(void) {
3766         dissector_handle_t snmp_tcp_handle;
3767
3768         snmp_handle = find_dissector("snmp");
3769
3770         dissector_add("udp.port", UDP_PORT_SNMP, snmp_handle);
3771         dissector_add("udp.port", UDP_PORT_SNMP_TRAP, snmp_handle);
3772         dissector_add("udp.port", UDP_PORT_SNMP_PATROL, snmp_handle);
3773         dissector_add("ethertype", ETHERTYPE_SNMP, snmp_handle);
3774         dissector_add("ipx.socket", IPX_SOCKET_SNMP_AGENT, snmp_handle);
3775         dissector_add("ipx.socket", IPX_SOCKET_SNMP_SINK, snmp_handle);
3776         dissector_add("hpext.dxsap", HPEXT_SNMP, snmp_handle);
3777
3778         snmp_tcp_handle = create_dissector_handle(dissect_snmp_tcp, proto_snmp);
3779         dissector_add("tcp.port", TCP_PORT_SNMP, snmp_tcp_handle);
3780         dissector_add("tcp.port", TCP_PORT_SNMP_TRAP, snmp_tcp_handle);
3781
3782         data_handle = find_dissector("data");
3783
3784         /*
3785          * Process preference settings.
3786          *
3787          * We can't do this in the register routine, as preferences aren't
3788          * read until all dissector register routines have been called (so
3789          * that all dissector preferences have been registered).
3790          */
3791         process_prefs();
3792
3793 }
3794
3795 void
3796 proto_register_smux(void)
3797 {
3798         static hf_register_info hf[] = {
3799                 { &hf_smux_version,
3800                 { "Version", "smux.version", FT_UINT8, BASE_DEC, NULL,
3801                     0x0, NULL, HFILL }},
3802                 { &hf_smux_pdutype,
3803                 { "PDU type", "smux.pdutype", FT_UINT8, BASE_DEC, VALS(smux_types),
3804                     0x0, NULL, HFILL }},
3805         };
3806         static gint *ett[] = {
3807                 &ett_smux,
3808         };
3809
3810         proto_smux = proto_register_protocol("SNMP Multiplex Protocol",
3811             "SMUX", "smux");
3812         proto_register_field_array(proto_smux, hf, array_length(hf));
3813         proto_register_subtree_array(ett, array_length(ett));
3814
3815 }
3816
3817 void
3818 proto_reg_handoff_smux(void)
3819 {
3820         dissector_handle_t smux_handle;
3821
3822         smux_handle = create_dissector_handle(dissect_smux, proto_smux);
3823         dissector_add("tcp.port", TCP_PORT_SMUX, smux_handle);
3824 }