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