8439b1ca7bb3e9ceb96146e33ae62d0de1b1b6ca
[obnox/wireshark/wip.git] / asn1 / snmp / packet-snmp-template.c
1 /* packet-snmp.c
2  * Routines for SNMP (simple network management protocol)
3  * Copyright (C) 1998 Didier Jorand
4  *
5  * See RFC 1157 for SNMPv1.
6  *
7  * See RFCs 1901, 1905, and 1906 for SNMPv2c.
8  *
9  * See RFCs 1905, 1906, 1909, and 1910 for SNMPv2u [historic].
10  *
11  * See RFCs 2570-2576 for SNMPv3
12  * Updated to use the asn2wrs compiler made by Tomas Kukosa
13  * Copyright (C) 2005 - 2006 Anders Broman [AT] ericsson.com
14  *
15  * See RFC 3414 for User-based Security Model for SNMPv3
16  * See RFC 3826 for  (AES) Cipher Algorithm in the SNMP USM
17  * See RFC 2578 for Structure of Management Information Version 2 (SMIv2)
18  * Copyright (C) 2007 Luis E. Garcia Ontanon <luis@ontanon.org>
19  *
20  * $Id$
21  *
22  * Wireshark - Network traffic analyzer
23  * By Gerald Combs <gerald@wireshark.org>
24  * Copyright 1998 Gerald Combs
25  *
26  * Some stuff from:
27  *
28  * GXSNMP -- An snmp mangament application
29  * Copyright (C) 1998 Gregory McLean & Jochen Friedrich
30  * Beholder RMON ethernet network monitor,Copyright (C) 1993 DNPAP group
31  *
32  * This program is free software; you can redistribute it and/or
33  * modify it under the terms of the GNU General Public License
34  * as published by the Free Software Foundation; either version 2
35  * of the License, or (at your option) any later version.
36  *
37  * This program is distributed in the hope that it will be useful,
38  * but WITHOUT ANY WARRANTY; without even the implied warranty of
39  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40  * GNU General Public License for more details.
41  *
42  * You should have received a copy of the GNU General Public License
43  * along with this program; if not, write to the Free Software
44  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
45  */
46
47 #define D(args) do {printf args; fflush(stdout); } while(0)
48
49 #ifdef HAVE_CONFIG_H
50 # include "config.h"
51 #endif
52
53 #include <string.h>
54 #include <ctype.h>
55
56 #include <glib.h>
57
58 #include <epan/packet.h>
59 #include <epan/strutil.h>
60 #include <epan/conversation.h>
61 #include <epan/etypes.h>
62 #include <epan/prefs.h>
63 #include <epan/sminmpec.h>
64 #include <epan/emem.h>
65 #include <epan/next_tvb.h>
66 #include <epan/uat.h>
67 #include <epan/asn1.h>
68 #include "packet-ipx.h"
69 #include "packet-hpext.h"
70
71
72 #include "packet-ber.h"
73
74 #include "packet-snmp.h"
75
76 #include <epan/crypt/crypt-sha1.h>
77 #include <epan/crypt/crypt-md5.h>
78 #include <epan/expert.h>
79 #include <epan/report_err.h>
80 #include <epan/oids.h>
81
82
83 #ifdef HAVE_LIBGCRYPT
84 #include <gcrypt.h>
85 #endif
86
87 /* Take a pointer that may be null and return a pointer that's not null
88    by turning null pointers into pointers to the above null string,
89    and, if the argument pointer wasn't null, make sure we handle
90    non-printable characters in the string by escaping them. */
91 #define SAFE_STRING(s, l)       (((s) != NULL) ? format_text((s), (l)) : "")
92
93 #define PNAME  "Simple Network Management Protocol"
94 #define PSNAME "SNMP"
95 #define PFNAME "snmp"
96
97 #define UDP_PORT_SNMP           161
98 #define UDP_PORT_SNMP_TRAP      162
99 #define TCP_PORT_SNMP           161
100 #define TCP_PORT_SNMP_TRAP      162
101 #define TCP_PORT_SMUX           199
102 #define UDP_PORT_SNMP_PATROL 8161
103
104 /* Initialize the protocol and registered fields */
105 static int proto_snmp = -1;
106 static int proto_smux = -1;
107
108 static gboolean display_oid = TRUE;
109 static gboolean snmp_var_in_tree = TRUE;
110
111 static gboolean snmp_usm_auth_md5(snmp_usm_params_t* p, guint8**, guint*, gchar const**);
112 static gboolean snmp_usm_auth_sha1(snmp_usm_params_t* p, guint8**, guint*, gchar const**);
113
114 static tvbuff_t* snmp_usm_priv_des(snmp_usm_params_t*, tvbuff_t*, gchar const**);
115 static tvbuff_t* snmp_usm_priv_aes(snmp_usm_params_t*, tvbuff_t*, gchar const**);
116
117
118 static void snmp_usm_password_to_key_md5(const guint8 *password, guint passwordlen, const guint8 *engineID, guint engineLength, guint8 *key);
119 static void snmp_usm_password_to_key_sha1(const guint8 *password, guint passwordlen, const guint8 *engineID, guint engineLength, guint8 *key);
120
121
122 static snmp_usm_auth_model_t model_md5 = {snmp_usm_password_to_key_md5, snmp_usm_auth_md5, 16};
123 static snmp_usm_auth_model_t model_sha1 = {snmp_usm_password_to_key_sha1, snmp_usm_auth_sha1, 20};
124
125 static const value_string auth_types[] = {
126         {0,"MD5"},
127         {1,"SHA1"},
128         {0,NULL}
129 };
130 static snmp_usm_auth_model_t* auth_models[] = {&model_md5,&model_sha1};
131
132
133 static const value_string priv_types[] = {
134         {0,"DES"},
135         {1,"AES"},
136         {0,NULL}
137 };
138 static snmp_usm_decoder_t priv_protos[] = {snmp_usm_priv_des, snmp_usm_priv_aes};
139
140 static snmp_ue_assoc_t* ueas = NULL;
141 static guint num_ueas = 0;
142 static snmp_ue_assoc_t* localized_ues = NULL;
143 static snmp_ue_assoc_t* unlocalized_ues = NULL;
144 /****/
145
146 /* Variabled used for handling enterprise spesific trap types */
147 typedef struct _snmp_st_assoc_t {
148         char *enterprise;
149         guint trap;
150         char *desc;
151 } snmp_st_assoc_t;
152 static guint num_specific_traps = 0;
153 static snmp_st_assoc_t *specific_traps = NULL;
154 static const char *enterprise_oid = NULL;
155 static guint generic_trap = 0;
156
157
158 static snmp_usm_params_t usm_p = {FALSE,FALSE,0,0,0,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,FALSE};
159
160 #define TH_AUTH   0x01
161 #define TH_CRYPT  0x02
162 #define TH_REPORT 0x04
163
164 /* desegmentation of SNMP-over-TCP */
165 static gboolean snmp_desegment = TRUE;
166
167 /* Global variables */
168
169 guint32 MsgSecurityModel;
170 tvbuff_t *oid_tvb=NULL;
171 tvbuff_t *value_tvb=NULL;
172
173 static dissector_handle_t snmp_handle;
174 static dissector_handle_t data_handle;
175
176 static next_tvb_list_t var_list;
177
178 static int hf_snmp_v3_flags_auth = -1;
179 static int hf_snmp_v3_flags_crypt = -1;
180 static int hf_snmp_v3_flags_report = -1;
181
182 static int hf_snmp_engineid_conform = -1;
183 static int hf_snmp_engineid_enterprise = -1;
184 static int hf_snmp_engineid_format = -1;
185 static int hf_snmp_engineid_ipv4 = -1;
186 static int hf_snmp_engineid_ipv6 = -1;
187 static int hf_snmp_engineid_cisco_type = -1;
188 static int hf_snmp_engineid_mac = -1;
189 static int hf_snmp_engineid_text = -1;
190 static int hf_snmp_engineid_time = -1;
191 static int hf_snmp_engineid_data = -1;
192 static int hf_snmp_decryptedPDU = -1;
193 static int hf_snmp_msgAuthentication = -1;
194
195 static int hf_snmp_noSuchObject = -1;
196 static int hf_snmp_noSuchInstance = -1;
197 static int hf_snmp_endOfMibView = -1;
198 static int hf_snmp_unSpecified = -1;
199
200 static int hf_snmp_integer32_value = -1;
201 static int hf_snmp_octetstring_value = -1;
202 static int hf_snmp_oid_value = -1;
203 static int hf_snmp_null_value = -1;
204 static int hf_snmp_ipv4_value = -1;
205 static int hf_snmp_ipv6_value = -1;
206 static int hf_snmp_anyaddress_value = -1;
207 static int hf_snmp_unsigned32_value = -1;
208 static int hf_snmp_unknown_value = -1;
209 static int hf_snmp_opaque_value = -1;
210 static int hf_snmp_nsap_value = -1;
211 static int hf_snmp_counter_value = -1;
212 static int hf_snmp_timeticks_value = -1;
213 static int hf_snmp_big_counter_value = -1;
214 static int hf_snmp_gauge32_value = -1;
215
216 static int hf_snmp_objectname = -1;
217 static int hf_snmp_scalar_instance_index = -1;
218
219
220 #include "packet-snmp-hf.c"
221
222 static int hf_smux_version = -1;
223 static int hf_smux_pdutype = -1;
224
225 /* Initialize the subtree pointers */
226 static gint ett_smux = -1;
227 static gint ett_snmp = -1;
228 static gint ett_engineid = -1;
229 static gint ett_msgFlags = -1;
230 static gint ett_encryptedPDU = -1;
231 static gint ett_decrypted = -1;
232 static gint ett_authParameters = -1;
233 static gint ett_internet = -1;
234 static gint ett_varbind = -1;
235 static gint ett_name = -1;
236 static gint ett_value = -1;
237 static gint ett_decoding_error = -1;
238
239 #include "packet-snmp-ett.c"
240
241 static const true_false_string auth_flags = {
242         "OK",
243         "Failed"
244 };
245
246 /* Security Models */
247
248 #define SNMP_SEC_ANY                    0
249 #define SNMP_SEC_V1                             1
250 #define SNMP_SEC_V2C                    2
251 #define SNMP_SEC_USM                    3
252
253 static const value_string sec_models[] = {
254         { SNMP_SEC_ANY,                 "Any" },
255         { SNMP_SEC_V1,                  "V1" },
256         { SNMP_SEC_V2C,                 "V2C" },
257         { SNMP_SEC_USM,                 "USM" },
258         { 0,                            NULL }
259 };
260
261 /* SMUX PDU types */
262 #define SMUX_MSG_OPEN           0
263 #define SMUX_MSG_CLOSE          1
264 #define SMUX_MSG_RREQ           2
265 #define SMUX_MSG_RRSP           3
266 #define SMUX_MSG_SOUT           4
267
268 static const value_string smux_types[] = {
269         { SMUX_MSG_OPEN,        "Open" },
270         { SMUX_MSG_CLOSE,       "Close" },
271         { SMUX_MSG_RREQ,        "Registration Request" },
272         { SMUX_MSG_RRSP,        "Registration Response" },
273         { SMUX_MSG_SOUT,        "Commit Or Rollback" },
274         { 0,                    NULL }
275 };
276
277
278 #define SNMP_IPA    0           /* IP Address */
279 #define SNMP_CNT    1           /* Counter (Counter32) */
280 #define SNMP_GGE    2           /* Gauge (Gauge32) */
281 #define SNMP_TIT    3           /* TimeTicks */
282 #define SNMP_OPQ    4           /* Opaque */
283 #define SNMP_NSP    5           /* NsapAddress */
284 #define SNMP_C64    6           /* Counter64 */
285 #define SNMP_U32    7           /* Uinteger32 */
286
287 #define SERR_NSO    0
288 #define SERR_NSI    1
289 #define SERR_EOM    2
290
291
292 dissector_table_t value_sub_dissectors_table;
293
294
295 static const gchar *
296 snmp_lookup_specific_trap (guint specific_trap)
297 {
298         guint i;
299
300         for (i = 0; i < num_specific_traps; i++) {
301                 snmp_st_assoc_t *u = &(specific_traps[i]);
302
303                 if ((u->trap == specific_trap) &&
304                     (strcmp (u->enterprise, enterprise_oid) == 0))
305                 {
306                         return u->desc;
307                 }
308         }
309
310         return NULL;
311 }
312
313 /*
314  *  dissect_snmp_VarBind
315  *  this routine dissects variable bindings, looking for the oid information in our oid reporsitory
316  *  to format and add the value adequatelly.
317  *
318  * The choice to handwrite this code instead of using the asn compiler is to avoid having tons
319  * of uses of global variables distributed in very different parts of the code.
320  * Other than that there's a cosmetic thing: the tree from ASN generated code would be so
321  * convoluted due to the nesting of CHOICEs in the definition of VarBind/value.
322  *
323  * XXX: the length of this function (~400 lines) is an aberration!
324  *  oid_key_t:key_type could become a series of callbacks instead of an enum
325  *  the (! oid_info_is_ok) switch could be made into an array (would be slower)
326  *
327
328         NetworkAddress ::=  CHOICE { internet IpAddress }
329         IpAddress ::= [APPLICATION 0] IMPLICIT OCTET STRING (SIZE (4))
330         TimeTicks ::= [APPLICATION 3] IMPLICIT INTEGER (0..4294967295)
331         Integer32 ::= INTEGER (-2147483648..2147483647)
332         ObjectName ::= OBJECT IDENTIFIER
333         Counter32 ::= [APPLICATION 1] IMPLICIT INTEGER (0..4294967295)
334         Gauge32 ::= [APPLICATION 2] IMPLICIT INTEGER (0..4294967295)
335         Unsigned32 ::= [APPLICATION 2] IMPLICIT INTEGER (0..4294967295)
336         Integer-value ::=  INTEGER (-2147483648..2147483647)
337         Integer32 ::= INTEGER (-2147483648..2147483647)
338         ObjectID-value ::= OBJECT IDENTIFIER
339         Empty ::= NULL
340         TimeTicks ::= [APPLICATION 3] IMPLICIT INTEGER (0..4294967295)
341         Opaque ::= [APPLICATION 4] IMPLICIT OCTET STRING
342         Counter64 ::= [APPLICATION 6] IMPLICIT INTEGER (0..18446744073709551615)
343
344         ObjectSyntax ::= CHOICE {
345                  simple SimpleSyntax,
346                  application-wide ApplicationSyntax
347         }
348
349         SimpleSyntax ::= CHOICE {
350            integer-value Integer-value,
351            string-value String-value,
352            objectID-value ObjectID-value,
353            empty  Empty
354         }
355
356         ApplicationSyntax ::= CHOICE {
357            ipAddress-value IpAddress,
358            counter-value Counter32,
359            timeticks-value TimeTicks,
360            arbitrary-value Opaque,
361            big-counter-value Counter64,
362            unsigned-integer-value Unsigned32
363         }
364
365         ValueType ::=  CHOICE {
366            value ObjectSyntax,
367            unSpecified NULL,
368            noSuchObject[0] IMPLICIT NULL,
369            noSuchInstance[1] IMPLICIT NULL,
370            endOfMibView[2] IMPLICIT NULL
371         }
372
373         VarBind ::= SEQUENCE {
374            name ObjectName,
375            valueType ValueType
376         }
377
378  */
379
380 extern int
381 dissect_snmp_VarBind(gboolean implicit_tag _U_, tvbuff_t *tvb, int offset,
382                      asn1_ctx_t *actx, proto_tree *tree, int hf_index _U_)
383 {
384         int seq_offset, name_offset, value_offset, value_start;
385         guint32 seq_len, name_len, value_len;
386         gint8 ber_class;
387         gboolean pc;
388         gint32 tag;
389         gboolean ind;
390         guint32* subids;
391         guint8* oid_bytes;
392         oid_info_t* oid_info = NULL;
393         guint oid_matched, oid_left;
394         proto_item *pi_name, *pi_varbind, *pi_value = NULL;
395         proto_tree *pt, *pt_varbind, *pt_name, *pt_value;
396         char label[ITEM_LABEL_LENGTH];
397         const char* repr = NULL;
398         const char* info_oid = NULL;
399         char* valstr;
400         int hfid = -1;
401         int min_len = 0, max_len = 0;
402         gboolean oid_info_is_ok;
403         const char* oid_string = NULL;
404         enum {BER_NO_ERROR, BER_WRONG_LENGTH, BER_WRONG_TAG} format_error = BER_NO_ERROR;
405
406         seq_offset = offset;
407
408         /* first have the VarBind's sequence header */
409         offset = get_ber_identifier(tvb, offset, &ber_class, &pc, &tag);
410         offset = get_ber_length(tvb, offset, &seq_len, &ind);
411
412         seq_len += offset - seq_offset;
413
414         if (!pc && ber_class==BER_CLASS_UNI && tag==BER_UNI_TAG_SEQUENCE) {
415                 proto_item* pi = proto_tree_add_text(tree, tvb, seq_offset, seq_len,"VarBind must be an universal class sequence");
416                 pt = proto_item_add_subtree(pi,ett_decoding_error);
417                 expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "VarBind is not an universal class sequence");
418                 return dissect_unknown_ber(actx->pinfo, tvb, seq_offset, pt);
419         }
420
421         if (ind) {
422                 proto_item* pi = proto_tree_add_text(tree, tvb, seq_offset, seq_len,"Indicator must be clear in VarBind");
423                 pt = proto_item_add_subtree(pi,ett_decoding_error);
424                 expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "VarBind has indicator set");
425                 return dissect_unknown_ber(actx->pinfo, tvb, seq_offset, pt);
426         }
427
428         /* then we have the ObjectName's header */
429
430         offset = get_ber_identifier(tvb, offset, &ber_class, &pc, &tag);
431         name_offset = offset = get_ber_length(tvb, offset, &name_len, &ind);
432
433         if (! ( !pc && ber_class==BER_CLASS_UNI && tag==BER_UNI_TAG_OID) ) {
434                 proto_item* pi = proto_tree_add_text(tree, tvb, seq_offset, seq_len,"ObjectName must be an OID in primitive encoding");
435                 pt = proto_item_add_subtree(pi,ett_decoding_error);
436                 expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "ObjectName not an OID");
437                 return dissect_unknown_ber(actx->pinfo, tvb, seq_offset, pt);
438         }
439
440         if (ind) {
441                 proto_item* pi = proto_tree_add_text(tree, tvb, seq_offset, seq_len,"Indicator must be clear in ObjectName");
442                 pt = proto_item_add_subtree(pi,ett_decoding_error);
443                 expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "ObjectName has indicator set");
444                 return dissect_unknown_ber(actx->pinfo, tvb, seq_offset, pt);
445         }
446
447         offset += name_len;
448         value_start = offset;
449
450         /* then we have the  value's header */
451         offset = get_ber_identifier(tvb, offset, &ber_class, &pc, &tag);
452         value_offset = get_ber_length(tvb, offset, &value_len, &ind);
453
454         if (! (!pc) ) {
455                 proto_item* pi = proto_tree_add_text(tree, tvb, seq_offset, seq_len,"the value must be in primitive encoding");
456                 pt = proto_item_add_subtree(pi,ett_decoding_error);
457                 expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "value not in primitive encoding");
458                 return dissect_unknown_ber(actx->pinfo, tvb, seq_offset, pt);
459         }
460
461         /* Now, we know where everithing is */
462
463
464
465         /* we add the varbind tree root with a dummy label we'll fill later on */
466         pi_varbind = proto_tree_add_text(tree,tvb,seq_offset,seq_len,"VarBind");
467         pt_varbind = proto_item_add_subtree(pi_varbind,ett_varbind);
468         *label = '\0';
469
470         pi_name = proto_tree_add_item(pt_varbind,hf_snmp_objectname,tvb,name_offset,name_len,ENC_NA);
471         pt_name = proto_item_add_subtree(pi_name,ett_name);
472
473         /* fetch ObjectName and its relative oid_info */
474         oid_bytes = ep_tvb_memdup(tvb, name_offset, name_len);
475         oid_info = oid_get_from_encoded(oid_bytes, name_len, &subids, &oid_matched, &oid_left);
476
477         add_oid_debug_subtree(oid_info,pt_name);
478
479         if (!subids) {
480                 proto_item* pi;
481
482                 repr = oid_encoded2string(oid_bytes, name_len);
483                 pi = proto_tree_add_text(pt_name,tvb, 0, 0, "invalid oid: %s", repr);
484                 pt = proto_item_add_subtree(pi, ett_decoding_error);
485                 expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "invalid oid: %s", repr);
486                 return dissect_unknown_ber(actx->pinfo, tvb, name_offset, pt);
487         }
488
489         if (oid_matched+oid_left) {
490                 oid_string = oid_subid2string(subids,oid_matched+oid_left);
491         }
492
493         if (ber_class == BER_CLASS_CON) {
494                 /* if we have an error value just add it and get out the way ASAP */
495                 proto_item* pi;
496                 const char* note;
497
498                 if (value_len != 0) {
499                         min_len = max_len = 0;
500                         format_error = BER_WRONG_LENGTH;
501                 }
502
503                 switch (tag) {
504                         case SERR_NSO:
505                                 hfid = hf_snmp_noSuchObject;
506                                 note = "noSuchObject";
507                                 break;
508                         case SERR_NSI:
509                                 hfid = hf_snmp_noSuchInstance;
510                                 note = "noSuchInstance";
511                                 break;
512                         case SERR_EOM:
513                                 hfid = hf_snmp_endOfMibView;
514                                 note = "endOfMibView";
515                                 break;
516                         default: {
517                                 pi = proto_tree_add_text(pt_varbind,tvb,0,0,"Wrong tag for Error Value: expected 0, 1, or 2 but got: %d",tag);
518                                 pt = proto_item_add_subtree(pi,ett_decoding_error);
519                                 expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "Wrong tag for SNMP VarBind error value");
520                                 return dissect_unknown_ber(actx->pinfo, tvb, value_start, pt);
521                         }
522                 }
523
524                 pi = proto_tree_add_item(pt_varbind,hfid,tvb,value_offset,value_len,ENC_BIG_ENDIAN);
525                 expert_add_info_format(actx->pinfo, pi, PI_RESPONSE_CODE, PI_NOTE, "%s",note);
526                 g_strlcpy (label, note, ITEM_LABEL_LENGTH);
527                 goto set_label;
528         }
529
530         /* 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 */
531         switch (oid_info->kind) {
532                 case OID_KIND_SCALAR:
533                         if (oid_left  == 1) {
534                                 /* OK: we got the instance sub-id */
535                                 proto_tree_add_uint64(pt_name,hf_snmp_scalar_instance_index,tvb,name_offset,name_len,subids[oid_matched]);
536                                 oid_info_is_ok = TRUE;
537                                 goto indexing_done;
538                         } else if (oid_left  == 0) {
539                                 if (ber_class == BER_CLASS_UNI && tag == BER_UNI_TAG_NULL) {
540                                         /* unSpecified  does not require an instance sub-id add the new value and get off the way! */
541                                         pi_value = proto_tree_add_item(pt_varbind,hf_snmp_unSpecified,tvb,value_offset,value_len,ENC_NA);
542                                         goto set_label;
543                                 } else {
544                                         proto_item* pi = proto_tree_add_text(pt_name,tvb,0,0,"A scalar should have one instance sub-id this one has none");
545                                         expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "No instance sub-id in scalar value");
546                                         oid_info_is_ok = FALSE;
547                                         goto indexing_done;
548                                 }
549                         } else {
550                                 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);
551                                 expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "Wrong number of instance sub-ids in scalar value");
552                                 oid_info_is_ok = FALSE;
553                                 goto indexing_done;
554                         }
555                 break;
556                 case OID_KIND_COLUMN:
557                         if ( oid_info->parent->kind == OID_KIND_ROW) {
558                                 oid_key_t* k = oid_info->parent->key;
559                                 guint key_start = oid_matched;
560                                 guint key_len = oid_left;
561                                 oid_info_is_ok = TRUE;
562
563                                 if ( key_len == 0 && ber_class == BER_CLASS_UNI && tag == BER_UNI_TAG_NULL) {
564                                         /* unSpecified  does not require an instance sub-id add the new value and get off the way! */
565                                         pi_value = proto_tree_add_item(pt_varbind,hf_snmp_unSpecified,tvb,value_offset,value_len,ENC_NA);
566                                         goto set_label;
567                                 }
568
569                                 if (k) {
570                                         for (;k;k = k->next) {
571                                                 guint suboid_len;
572
573                                                 if (key_start >= oid_matched+oid_left) {
574                                                         proto_item* pi = proto_tree_add_text(pt_name,tvb,0,0,"index sub-oid shorter than expected");
575                                                         expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "index sub-oid shorter than expected");
576                                                         oid_info_is_ok = FALSE;
577                                                         goto indexing_done;
578                                                 }
579
580                                                 switch(k->key_type) {
581                                                         case OID_KEY_TYPE_WRONG: {
582                                                                 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");
583                                                                 expert_add_info_format(actx->pinfo, pi, PI_UNDECODED, PI_WARN, "Unimplemented instance index");
584                                                                 oid_info_is_ok = FALSE;
585                                                                 goto indexing_done;
586                                                         }
587                                                         case OID_KEY_TYPE_INTEGER: {
588                                                                 if (IS_FT_INT(k->ft_type)) {
589                                                                         proto_tree_add_int(pt_name,k->hfid,tvb,name_offset,name_len,(guint)subids[key_start]);
590                                                                 } else { /* if it's not an unsigned int let proto_tree_add_uint throw a warning */
591                                                                         proto_tree_add_uint64(pt_name,k->hfid,tvb,name_offset,name_len,(guint)subids[key_start]);
592                                                                 }
593                                                                 key_start++;
594                                                                 key_len--;
595                                                                 continue; /* k->next */
596                                                         }
597                                                         case OID_KEY_TYPE_IMPLIED_OID:
598                                                                 suboid_len = key_len;
599
600                                                                 goto show_oid_index;
601
602                                                         case OID_KEY_TYPE_OID: {
603                                                                 guint8* suboid_buf;
604                                                                 guint suboid_buf_len;
605                                                                 guint32* suboid;
606
607                                                                 suboid_len = subids[key_start++];
608                                                                 key_len--;
609
610 show_oid_index:
611                                                                 suboid = &(subids[key_start]);
612
613                                                                 if( suboid_len == 0 ) {
614                                                                         proto_item* pi = proto_tree_add_text(pt_name,tvb,0,0,"an index sub-oid OID cannot be 0 bytes long!");
615                                                                         expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "index sub-oid OID with len=0");
616                                                                         oid_info_is_ok = FALSE;
617                                                                         goto indexing_done;
618                                                                 }
619
620                                                                 if( key_len < suboid_len ) {
621                                                                         proto_item* pi = proto_tree_add_text(pt_name,tvb,0,0,"index sub-oid should not be longer than remaining oid size");
622                                                                         expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "index sub-oid longer than remaining oid size");
623                                                                         oid_info_is_ok = FALSE;
624                                                                         goto indexing_done;
625                                                                 }
626
627                                                                 suboid_buf_len = oid_subid2encoded(suboid_len, suboid, &suboid_buf);
628
629                                                                 DISSECTOR_ASSERT(suboid_buf_len);
630
631                                                                 proto_tree_add_oid(pt_name,k->hfid,tvb,name_offset, suboid_buf_len, suboid_buf);
632
633                                                                 key_start += suboid_len;
634                                                                 key_len -= suboid_len + 1;
635                                                                 continue; /* k->next */
636                                                         }
637                                                         default: {
638                                                                 guint8* buf;
639                                                                 guint buf_len;
640                                                                 guint32* suboid;
641                                                                 guint i;
642
643
644                                                                 switch (k->key_type) {
645                                                                         case OID_KEY_TYPE_IPADDR:
646                                                                                 suboid = &(subids[key_start]);
647                                                                                 buf_len = 4;
648                                                                                 break;
649                                                                         case OID_KEY_TYPE_IMPLIED_STRING:
650                                                                         case OID_KEY_TYPE_IMPLIED_BYTES:
651                                                                         case OID_KEY_TYPE_ETHER:
652                                                                                 suboid = &(subids[key_start]);
653                                                                                 buf_len = key_len;
654                                                                                 break;
655                                                                         default:
656                                                                                 buf_len = k->num_subids;
657                                                                                 suboid = &(subids[key_start]);
658
659                                                                                 if(!buf_len) {
660                                                                                         buf_len = *suboid++;
661                                                                                         key_len--;
662                                                                                         key_start++;
663                                                                                 }
664                                                                                 break;
665                                                                 }
666
667                                                                 if( key_len < buf_len ) {
668                                                                         proto_item* pi = proto_tree_add_text(pt_name,tvb,0,0,"index string should not be longer than remaining oid size");
669                                                                         expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "index string longer than remaining oid size");
670                                                                         oid_info_is_ok = FALSE;
671                                                                         goto indexing_done;
672                                                                 }
673
674                                                                 buf = ep_alloc(buf_len+1);
675                                                                 for (i = 0; i < buf_len; i++)
676                                                                         buf[i] = (guint8)suboid[i];
677                                                                 buf[i] = '\0';
678
679                                                                 switch(k->key_type) {
680                                                                         case OID_KEY_TYPE_STRING:
681                                                                         case OID_KEY_TYPE_IMPLIED_STRING:
682                                                                                 proto_tree_add_string(pt_name,k->hfid,tvb,name_offset,buf_len, buf);
683                                                                                 break;
684                                                                         case OID_KEY_TYPE_BYTES:
685                                                                         case OID_KEY_TYPE_NSAP:
686                                                                         case OID_KEY_TYPE_IMPLIED_BYTES:
687                                                                                 proto_tree_add_bytes(pt_name,k->hfid,tvb,name_offset,buf_len, buf);
688                                                                                 break;
689                                                                         case OID_KEY_TYPE_ETHER:
690                                                                                 proto_tree_add_ether(pt_name,k->hfid,tvb,name_offset,buf_len, buf);
691                                                                                 break;
692                                                                         case OID_KEY_TYPE_IPADDR: {
693                                                                                 guint32* ipv4_p = (void*)buf;
694                                                                                 proto_tree_add_ipv4(pt_name,k->hfid,tvb,name_offset,buf_len, *ipv4_p);
695                                                                                 }
696                                                                                 break;
697                                                                         default:
698                                                                                 DISSECTOR_ASSERT_NOT_REACHED();
699                                                                                 break;
700                                                                 }
701
702                                                                 key_start += buf_len;
703                                                                 key_len -= buf_len;
704                                                                 continue; /* k->next*/
705                                                         }
706                                                 }
707                                         }
708                                         goto indexing_done;
709                                 } else {
710                                         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");
711                                         expert_add_info_format(actx->pinfo, pi, PI_UNDECODED, PI_WARN, "Unimplemented instance index");
712                                         oid_info_is_ok = FALSE;
713                                         goto indexing_done;
714                                 }
715                         } else {
716                                 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.");
717                                 expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_ERROR, "COLUMS's parent is not a ROW");
718                                 oid_info_is_ok = FALSE;
719                                 goto indexing_done;
720                         }
721                 default: {
722 /*                      proto_item* pi = proto_tree_add_text(pt_name,tvb,0,0,"This kind OID should have no value");
723                         expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "This kind OID should have no value"); */
724                         oid_info_is_ok = FALSE;
725                         goto indexing_done;
726                 }
727         }
728 indexing_done:
729
730         if (oid_info_is_ok && oid_info->value_type) {
731                 if (ber_class == BER_CLASS_UNI && tag == BER_UNI_TAG_NULL) {
732                         pi_value = proto_tree_add_item(pt_varbind,hf_snmp_unSpecified,tvb,value_offset,value_len,ENC_NA);
733                 } else {
734                         /* Provide a tree_item to attach errors to, if needed. */
735                         pi_value = pi_name;
736
737                         if ((oid_info->value_type->ber_class != BER_CLASS_ANY) &&
738                                 (ber_class != oid_info->value_type->ber_class))
739                                 format_error = BER_WRONG_TAG;
740                         else if ((oid_info->value_type->ber_tag != BER_TAG_ANY) &&
741                                 (tag != oid_info->value_type->ber_tag))
742                                 format_error = BER_WRONG_TAG;
743                         else {
744                                 max_len = oid_info->value_type->max_len == -1 ? 0xffffff : oid_info->value_type->max_len;
745                                 min_len  = oid_info->value_type->min_len;
746
747                                 if ((int)value_len < min_len || (int)value_len > max_len)
748                                         format_error = BER_WRONG_LENGTH;
749                         }
750
751                         if (format_error == BER_NO_ERROR)
752                                 pi_value = proto_tree_add_item(pt_varbind,oid_info->value_hfid,tvb,value_offset,value_len,ENC_BIG_ENDIAN);
753                 }
754         } else {
755                 switch(ber_class|(tag<<4)) {
756                         case BER_CLASS_UNI|(BER_UNI_TAG_INTEGER<<4):
757                         {
758                                 gint64 val=0;
759                                 unsigned int_val_offset = value_offset;
760                                 unsigned i;
761
762                                 max_len = 5; min_len = 1;
763                                 if (value_len > (guint)max_len && value_len < (guint)min_len) {
764                                         format_error = BER_WRONG_LENGTH;
765                                         break;
766                                 }
767
768                                 if(value_len > 0) {
769                                         /* extend sign bit */
770                                         if(tvb_get_guint8(tvb, int_val_offset)&0x80) {
771                                                 val=-1;
772                                         }
773                                         for(i=0;i<value_len;i++) {
774                                                 val=(val<<8)|tvb_get_guint8(tvb, int_val_offset);
775                                                 int_val_offset++;
776                                         }
777                                 }
778                                 proto_tree_add_int64(pt_varbind, hf_snmp_integer32_value, tvb,value_offset,value_len, val);
779
780                                 goto already_added;
781                         }
782                         case BER_CLASS_UNI|(BER_UNI_TAG_OCTETSTRING<<4):
783                                 hfid = hf_snmp_octetstring_value;
784                                 break;
785                         case BER_CLASS_UNI|(BER_UNI_TAG_OID<<4):
786                                 max_len = -1; min_len = 1;
787                                 if (value_len < (guint)min_len) format_error = BER_WRONG_LENGTH;
788                                 hfid = hf_snmp_oid_value;
789                                 break;
790                         case BER_CLASS_UNI|(BER_UNI_TAG_NULL<<4):
791                                 max_len = 0; min_len = 0;
792                                 if (value_len != 0) format_error = BER_WRONG_LENGTH;
793                                 hfid = hf_snmp_null_value;
794                                 break;
795                         case BER_CLASS_APP: /* | (SNMP_IPA<<4)*/
796                                 switch(value_len) {
797                                         case 4: hfid = hf_snmp_ipv4_value; break;
798                                         case 16: hfid = hf_snmp_ipv6_value; break;
799                                         default: hfid = hf_snmp_anyaddress_value; break;
800                                 }
801                                 break;
802                         case BER_CLASS_APP|(SNMP_U32<<4):
803                                 hfid = hf_snmp_unsigned32_value;
804                                 break;
805                         case BER_CLASS_APP|(SNMP_GGE<<4):
806                                 hfid = hf_snmp_gauge32_value;
807                                 break;
808                         case BER_CLASS_APP|(SNMP_CNT<<4):
809                                 hfid = hf_snmp_counter_value;
810                                 break;
811                         case BER_CLASS_APP|(SNMP_TIT<<4):
812                                 hfid = hf_snmp_timeticks_value;
813                                 break;
814                         case BER_CLASS_APP|(SNMP_OPQ<<4):
815                                 hfid = hf_snmp_opaque_value;
816                                 break;
817                         case BER_CLASS_APP|(SNMP_NSP<<4):
818                                 hfid = hf_snmp_nsap_value;
819                                 break;
820                         case BER_CLASS_APP|(SNMP_C64<<4):
821                                 hfid = hf_snmp_big_counter_value;
822                                 break;
823                         default:
824                                 hfid = hf_snmp_unknown_value;
825                                 break;
826                 }
827
828                 pi_value = proto_tree_add_item(pt_varbind,hfid,tvb,value_offset,value_len,ENC_BIG_ENDIAN);
829                 if (format_error != BER_NO_ERROR) {
830                         expert_add_info_format(actx->pinfo, pi_value, PI_UNDECODED, PI_NOTE, "Unresolved value, Missing MIB");
831                 }
832
833 already_added:
834                 oid_info_is_ok = FALSE;
835         }
836
837         pt_value = proto_item_add_subtree(pi_value,ett_value);
838
839         if (value_len > 0 && oid_string) {
840                 tvbuff_t* sub_tvb = tvb_new_subset(tvb, value_offset, value_len, value_len);
841
842                 next_tvb_add_string(&var_list, sub_tvb, (snmp_var_in_tree) ? pt_value : NULL, value_sub_dissectors_table, oid_string);
843         }
844
845
846 set_label:
847         if (pi_value) proto_item_fill_label(PITEM_FINFO(pi_value), label);
848
849         if (oid_info && oid_info->name) {
850                 if (oid_left >= 1) {
851                         repr  = ep_strdup_printf("%s.%s (%s)", oid_info->name,
852                                                  oid_subid2string(&(subids[oid_matched]),oid_left),
853                                                  oid_subid2string(subids,oid_matched+oid_left));
854                         info_oid = ep_strdup_printf("%s.%s", oid_info->name,
855                                                     oid_subid2string(&(subids[oid_matched]),oid_left));
856                 } else {
857                         repr  = ep_strdup_printf("%s (%s)", oid_info->name,
858                                                  oid_subid2string(subids,oid_matched));
859                         info_oid = oid_info->name;
860                 }
861         } else if (oid_string) {
862                 repr  = ep_strdup(oid_string);
863                 info_oid = oid_string;
864         } else {
865                 repr  = ep_strdup("[Bad OID]");
866         }
867
868         valstr = strstr(label,": ");
869         valstr = valstr ? valstr+2 : label;
870
871         proto_item_set_text(pi_varbind,"%s: %s",repr,valstr);
872
873         if (display_oid && info_oid) {
874           col_append_fstr (actx->pinfo->cinfo, COL_INFO, " %s", info_oid);
875         }
876
877         switch (format_error) {
878                 case BER_WRONG_LENGTH: {
879                         proto_tree* pt = proto_item_add_subtree(pi_value,ett_decoding_error);
880                         proto_item* pi = proto_tree_add_text(pt,tvb,0,0,"Wrong value length: %u  expecting: %u <= len <= %u",
881                                                              value_len, min_len, max_len == -1 ? 0xFFFFFF : max_len);
882                         pt = proto_item_add_subtree(pi,ett_decoding_error);
883                         expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "Wrong length for SNMP VarBind/value");
884                         return dissect_unknown_ber(actx->pinfo, tvb, value_start, pt);
885                 }
886                 case BER_WRONG_TAG: {
887                         proto_tree* pt = proto_item_add_subtree(pi_value,ett_decoding_error);
888                         proto_item* pi = proto_tree_add_text(pt,tvb,0,0,"Wrong class/tag for Value expected: %d,%d got: %d,%d",
889                                                              oid_info->value_type->ber_class, oid_info->value_type->ber_tag,
890                                                              ber_class, tag);
891                         pt = proto_item_add_subtree(pi,ett_decoding_error);
892                         expert_add_info_format(actx->pinfo, pi, PI_MALFORMED, PI_WARN, "Wrong class/tag for SNMP VarBind/value");
893                         return dissect_unknown_ber(actx->pinfo, tvb, value_start, pt);
894                 }
895                 default:
896                         break;
897         }
898
899         return seq_offset + seq_len;
900 }
901
902
903 #define F_SNMP_ENGINEID_CONFORM 0x80
904 #define SNMP_ENGINEID_RFC1910 0x00
905 #define SNMP_ENGINEID_RFC3411 0x01
906
907 static const true_false_string tfs_snmp_engineid_conform = {
908   "RFC3411 (SNMPv3)",
909   "RFC1910 (Non-SNMPv3)"
910 };
911
912 #define SNMP_ENGINEID_FORMAT_IPV4 0x01
913 #define SNMP_ENGINEID_FORMAT_IPV6 0x02
914 #define SNMP_ENGINEID_FORMAT_MACADDRESS 0x03
915 #define SNMP_ENGINEID_FORMAT_TEXT 0x04
916 #define SNMP_ENGINEID_FORMAT_OCTETS 0x05
917
918 static const value_string snmp_engineid_format_vals[] = {
919         { SNMP_ENGINEID_FORMAT_IPV4,    "IPv4 address" },
920         { SNMP_ENGINEID_FORMAT_IPV6,    "IPv6 address" },
921         { SNMP_ENGINEID_FORMAT_MACADDRESS,      "MAC address" },
922         { SNMP_ENGINEID_FORMAT_TEXT,    "Text, administratively assigned" },
923         { SNMP_ENGINEID_FORMAT_OCTETS,  "Octets, administratively assigned" },
924         { 0,    NULL }
925 };
926
927 #define SNMP_ENGINEID_CISCO_AGENT 0x00
928 #define SNMP_ENGINEID_CISCO_MANAGER 0x01
929
930 static const value_string snmp_engineid_cisco_type_vals[] = {
931         { SNMP_ENGINEID_CISCO_AGENT,    "Agent" },
932         { SNMP_ENGINEID_CISCO_MANAGER,  "Manager" },
933         { 0,    NULL }
934 };
935
936 /*
937  * SNMP Engine ID dissection according to RFC 3411 (SnmpEngineID TC)
938  * or historic RFC 1910 (AgentID)
939  */
940 int
941 dissect_snmp_engineid(proto_tree *tree, tvbuff_t *tvb, int offset, int len)
942 {
943     proto_item *item = NULL;
944     guint8 conformance, format;
945     guint32 enterpriseid, seconds;
946     nstime_t ts;
947     int len_remain = len;
948
949     /* first bit: engine id conformance */
950     if (len_remain<4) return offset;
951     conformance = ((tvb_get_guint8(tvb, offset)>>7) & 0x01);
952     proto_tree_add_item(tree, hf_snmp_engineid_conform, tvb, offset, 1, ENC_BIG_ENDIAN);
953
954     /* 4-byte enterprise number/name */
955     if (len_remain<4) return offset;
956     enterpriseid = tvb_get_ntohl(tvb, offset);
957     if (conformance)
958       enterpriseid -= 0x80000000; /* ignore first bit */
959     proto_tree_add_uint(tree, hf_snmp_engineid_enterprise, tvb, offset, 4, enterpriseid);
960     offset+=4;
961     len_remain-=4;
962
963     switch(conformance) {
964
965     case SNMP_ENGINEID_RFC1910:
966       /* 12-byte AgentID w/ 8-byte trailer */
967       if (len_remain==8) {
968         proto_tree_add_text(tree, tvb, offset, 8, "AgentID Trailer: 0x%s",
969                             tvb_bytes_to_str(tvb, offset, 8));
970         offset+=8;
971         len_remain-=8;
972       } else {
973         proto_tree_add_text(tree, tvb, offset, len_remain, "<Data not conforming to RFC1910>");
974         return offset;
975       }
976       break;
977
978     case SNMP_ENGINEID_RFC3411: /* variable length: 5..32 */
979
980       /* 1-byte format specifier */
981       if (len_remain<1) return offset;
982       format = tvb_get_guint8(tvb, offset);
983       item = proto_tree_add_uint_format(tree, hf_snmp_engineid_format, tvb, offset, 1, format, "Engine ID Format: %s (%d)",
984                                         val_to_str(format, snmp_engineid_format_vals, "Reserved/Enterprise-specific"), format);
985       offset+=1;
986       len_remain-=1;
987
988       switch(format) {
989       case SNMP_ENGINEID_FORMAT_IPV4:
990         /* 4-byte IPv4 address */
991         if (len_remain==4) {
992           proto_tree_add_item(tree, hf_snmp_engineid_ipv4, tvb, offset, 4, ENC_BIG_ENDIAN);
993           offset+=4;
994           len_remain=0;
995         }
996         break;
997       case SNMP_ENGINEID_FORMAT_IPV6:
998         /* 16-byte IPv6 address */
999         if (len_remain==16) {
1000           proto_tree_add_item(tree, hf_snmp_engineid_ipv6, tvb, offset, 16, ENC_NA);
1001           offset+=16;
1002           len_remain=0;
1003         }
1004         break;
1005       case SNMP_ENGINEID_FORMAT_MACADDRESS:
1006         /* See: https://supportforums.cisco.com/message/3010617#3010617 for details. */
1007         if ((enterpriseid==9)&&(len_remain==7)) {
1008           proto_tree_add_item(tree, hf_snmp_engineid_cisco_type, tvb, offset, 1, ENC_BIG_ENDIAN);
1009           offset++;
1010           len_remain--;
1011         }
1012         /* 6-byte MAC address */
1013         if (len_remain==6) {
1014           proto_tree_add_item(tree, hf_snmp_engineid_mac, tvb, offset, 6, ENC_BIG_ENDIAN);
1015           offset+=6;
1016           len_remain=0;
1017         }
1018         break;
1019       case SNMP_ENGINEID_FORMAT_TEXT:
1020         /* max. 27-byte string, administratively assigned */
1021         if (len_remain<=27) {
1022           proto_tree_add_item(tree, hf_snmp_engineid_text, tvb, offset, len_remain, ENC_BIG_ENDIAN);
1023           offset+=len_remain;
1024           len_remain=0;
1025         }
1026         break;
1027       case 128:
1028         /* most common enterprise-specific format: (ucd|net)-snmp random */
1029         if ((enterpriseid==2021)||(enterpriseid==8072)) {
1030           proto_item_append_text(item, (enterpriseid==2021) ? ": UCD-SNMP Random" : ": Net-SNMP Random");
1031           /* demystify: 4B random, 4B epoch seconds */
1032           if (len_remain==8) {
1033             proto_tree_add_item(tree, hf_snmp_engineid_data, tvb, offset, 4, ENC_NA);
1034             seconds = tvb_get_letohl(tvb, offset+4);
1035             ts.secs = seconds;
1036             ts.nsecs = 0;
1037             proto_tree_add_time_format_value(tree, hf_snmp_engineid_time, tvb, offset+4, 4,
1038                                              &ts, "%s",
1039                                              abs_time_secs_to_str(seconds, ABSOLUTE_TIME_LOCAL, TRUE));
1040             offset+=8;
1041             len_remain=0;
1042           }
1043         }
1044         break;
1045       case SNMP_ENGINEID_FORMAT_OCTETS:
1046       default:
1047         /* max. 27 bytes, administratively assigned or unknown format */
1048         if (len_remain<=27) {
1049           proto_tree_add_item(tree, hf_snmp_engineid_data, tvb, offset, len_remain, ENC_NA);
1050           offset+=len_remain;
1051           len_remain=0;
1052         }
1053         break;
1054       }
1055     }
1056
1057     if (len_remain>0) {
1058       proto_tree_add_text(tree, tvb, offset, len_remain, "<Data not conforming to RFC3411>");
1059       offset+=len_remain;
1060     }
1061     return offset;
1062 }
1063
1064
1065 static void set_ue_keys(snmp_ue_assoc_t* n ) {
1066         guint key_size = n->user.authModel->key_size;
1067
1068         n->user.authKey.data = se_alloc(key_size);
1069         n->user.authKey.len = key_size;
1070         n->user.authModel->pass2key(n->user.authPassword.data,
1071                                     n->user.authPassword.len,
1072                                     n->engine.data,
1073                                     n->engine.len,
1074                                     n->user.authKey.data);
1075
1076         n->user.privKey.data = se_alloc(key_size);
1077         n->user.privKey.len = key_size;
1078         n->user.authModel->pass2key(n->user.privPassword.data,
1079                                     n->user.privPassword.len,
1080                                     n->engine.data,
1081                                     n->engine.len,
1082                                     n->user.privKey.data);
1083 }
1084
1085 static snmp_ue_assoc_t*
1086 ue_se_dup(snmp_ue_assoc_t* o)
1087 {
1088         snmp_ue_assoc_t* d = se_memdup(o,sizeof(snmp_ue_assoc_t));
1089
1090         d->user.authModel = o->user.authModel;
1091
1092         d->user.privProtocol = o->user.privProtocol;
1093
1094         d->user.userName.data = se_memdup(o->user.userName.data,o->user.userName.len);
1095         d->user.userName.len = o->user.userName.len;
1096
1097         d->user.authPassword.data = o->user.authPassword.data ? se_memdup(o->user.authPassword.data,o->user.authPassword.len) : NULL;
1098         d->user.authPassword.len = o->user.authPassword.len;
1099
1100         d->user.privPassword.data = o->user.privPassword.data ? se_memdup(o->user.privPassword.data,o->user.privPassword.len) : NULL;
1101         d->user.privPassword.len = o->user.privPassword.len;
1102
1103         d->engine.len = o->engine.len;
1104
1105         if (d->engine.len) {
1106                 d->engine.data = se_memdup(o->engine.data,o->engine.len);
1107                 set_ue_keys(d);
1108         }
1109
1110         return d;
1111
1112 }
1113
1114
1115 #define CACHE_INSERT(c,a) if (c) { snmp_ue_assoc_t* t = c; c = a; c->next = t; } else { c = a; a->next = NULL; }
1116
1117 static void
1118 renew_ue_cache(void)
1119 {
1120         localized_ues = NULL;
1121         unlocalized_ues = NULL;
1122
1123         if (num_ueas) {
1124                 guint i;
1125
1126                 for(i = 0; i < num_ueas; i++) {
1127                         snmp_ue_assoc_t* a = ue_se_dup(&(ueas[i]));
1128
1129                         if (a->engine.len) {
1130                                 CACHE_INSERT(localized_ues,a);
1131
1132                         } else {
1133                                 CACHE_INSERT(unlocalized_ues,a);
1134                         }
1135
1136                 }
1137         }
1138 }
1139
1140
1141 static snmp_ue_assoc_t*
1142 localize_ue( snmp_ue_assoc_t* o, const guint8* engine, guint engine_len )
1143 {
1144         snmp_ue_assoc_t* n = se_memdup(o,sizeof(snmp_ue_assoc_t));
1145
1146         n->engine.data = se_memdup(engine,engine_len);
1147         n->engine.len = engine_len;
1148
1149         set_ue_keys(n);
1150
1151         return n;
1152 }
1153
1154
1155 #define localized_match(a,u,ul,e,el) \
1156         ( a->user.userName.len == ul \
1157         && a->engine.len == el \
1158         && memcmp( a->user.userName.data, u, ul ) == 0 \
1159         && memcmp( a->engine.data,   e,  el ) == 0 )
1160
1161 #define unlocalized_match(a,u,l) \
1162         ( a->user.userName.len == l && memcmp( a->user.userName.data, u, l) == 0 )
1163
1164 static snmp_ue_assoc_t*
1165 get_user_assoc(tvbuff_t* engine_tvb, tvbuff_t* user_tvb)
1166 {
1167         static snmp_ue_assoc_t* a;
1168         guint given_username_len;
1169         guint8* given_username;
1170         guint given_engine_len;
1171         guint8* given_engine;
1172
1173         if ( ! (localized_ues || unlocalized_ues ) ) return NULL;
1174
1175         if (! ( user_tvb && engine_tvb ) ) return NULL;
1176
1177         given_username_len = tvb_ensure_length_remaining(user_tvb,0);
1178         given_username = ep_tvb_memdup(user_tvb,0,-1);
1179         given_engine_len = tvb_ensure_length_remaining(engine_tvb,0);
1180         given_engine = ep_tvb_memdup(engine_tvb,0,-1);
1181
1182         for (a = localized_ues; a; a = a->next) {
1183                 if ( localized_match(a, given_username, given_username_len, given_engine, given_engine_len) ) {
1184                         return a;
1185                 }
1186         }
1187
1188         for (a = unlocalized_ues; a; a = a->next) {
1189                 if ( unlocalized_match(a, given_username, given_username_len) ) {
1190                         snmp_ue_assoc_t* n = localize_ue( a, given_engine, given_engine_len );
1191                         CACHE_INSERT(localized_ues,n);
1192                         return n;
1193                 }
1194         }
1195
1196         return NULL;
1197 }
1198
1199 static gboolean
1200 snmp_usm_auth_md5(snmp_usm_params_t* p, guint8** calc_auth_p, guint* calc_auth_len_p, gchar const** error)
1201 {
1202         guint msg_len;
1203         guint8* msg;
1204         guint auth_len;
1205         guint8* auth;
1206         guint8* key;
1207         guint key_len;
1208         guint8 *calc_auth;
1209         guint start;
1210         guint end;
1211         guint i;
1212
1213         if (!p->auth_tvb) {
1214                 *error = "No Authenticator";
1215                 return FALSE;
1216         }
1217
1218         key = p->user_assoc->user.authKey.data;
1219         key_len = p->user_assoc->user.authKey.len;
1220
1221         if (! key ) {
1222                 *error = "User has no authKey";
1223                 return FALSE;
1224         }
1225
1226
1227         auth_len = tvb_length_remaining(p->auth_tvb,0);
1228
1229         if (auth_len != 12) {
1230                 *error = "Authenticator length wrong";
1231                 return FALSE;
1232         }
1233
1234         msg_len = tvb_length_remaining(p->msg_tvb,0);
1235         msg = ep_tvb_memdup(p->msg_tvb,0,msg_len);
1236
1237
1238         auth = ep_tvb_memdup(p->auth_tvb,0,auth_len);
1239
1240         start = p->auth_offset - p->start_offset;
1241         end =   start + auth_len;
1242
1243         /* fill the authenticator with zeros */
1244         for ( i = start ; i < end ; i++ ) {
1245                 msg[i] = '\0';
1246         }
1247
1248         calc_auth = ep_alloc(16);
1249
1250         md5_hmac(msg, msg_len, key, key_len, calc_auth);
1251
1252         if (calc_auth_p) *calc_auth_p = calc_auth;
1253         if (calc_auth_len_p) *calc_auth_len_p = 12;
1254
1255         return ( memcmp(auth,calc_auth,12) != 0 ) ? FALSE : TRUE;
1256 }
1257
1258
1259 static gboolean
1260 snmp_usm_auth_sha1(snmp_usm_params_t* p _U_, guint8** calc_auth_p, guint* calc_auth_len_p,  gchar const** error _U_)
1261 {
1262         guint msg_len;
1263         guint8* msg;
1264         guint auth_len;
1265         guint8* auth;
1266         guint8* key;
1267         guint key_len;
1268         guint8 *calc_auth;
1269         guint start;
1270         guint end;
1271         guint i;
1272
1273         if (!p->auth_tvb) {
1274                 *error = "No Authenticator";
1275                 return FALSE;
1276         }
1277
1278         key = p->user_assoc->user.authKey.data;
1279         key_len = p->user_assoc->user.authKey.len;
1280
1281         if (! key ) {
1282                 *error = "User has no authKey";
1283                 return FALSE;
1284         }
1285
1286
1287         auth_len = tvb_length_remaining(p->auth_tvb,0);
1288
1289
1290         if (auth_len != 12) {
1291                 *error = "Authenticator length wrong";
1292                 return FALSE;
1293         }
1294
1295         msg_len = tvb_length_remaining(p->msg_tvb,0);
1296         msg = ep_tvb_memdup(p->msg_tvb,0,msg_len);
1297
1298         auth = ep_tvb_memdup(p->auth_tvb,0,auth_len);
1299
1300         start = p->auth_offset - p->start_offset;
1301         end =   start + auth_len;
1302
1303         /* fill the authenticator with zeros */
1304         for ( i = start ; i < end ; i++ ) {
1305                 msg[i] = '\0';
1306         }
1307
1308         calc_auth = ep_alloc(20);
1309
1310         sha1_hmac(key, key_len, msg, msg_len, calc_auth);
1311
1312         if (calc_auth_p) *calc_auth_p = calc_auth;
1313         if (calc_auth_len_p) *calc_auth_len_p = 12;
1314
1315         return ( memcmp(auth,calc_auth,12) != 0 ) ? FALSE : TRUE;
1316 }
1317
1318 static tvbuff_t*
1319 snmp_usm_priv_des(snmp_usm_params_t* p _U_, tvbuff_t* encryptedData _U_, gchar const** error _U_)
1320 {
1321 #ifdef HAVE_LIBGCRYPT
1322     gcry_error_t err;
1323     gcry_cipher_hd_t hd = NULL;
1324
1325         guint8* cleartext;
1326         guint8* des_key = p->user_assoc->user.privKey.data; /* first 8 bytes */
1327         guint8* pre_iv = &(p->user_assoc->user.privKey.data[8]); /* last 8 bytes */
1328         guint8* salt;
1329         gint salt_len;
1330         gint cryptgrm_len;
1331         guint8* cryptgrm;
1332         tvbuff_t* clear_tvb;
1333         guint8 iv[8];
1334         guint i;
1335
1336
1337         salt_len = tvb_length_remaining(p->priv_tvb,0);
1338
1339         if (salt_len != 8)  {
1340                 *error = "decryptionError: msgPrivacyParameters length != 8";
1341                 return NULL;
1342         }
1343
1344         salt = ep_tvb_memdup(p->priv_tvb,0,salt_len);
1345
1346         /*
1347          The resulting "salt" is XOR-ed with the pre-IV to obtain the IV.
1348          */
1349         for (i=0; i<8; i++) {
1350                 iv[i] = pre_iv[i] ^ salt[i];
1351         }
1352
1353         cryptgrm_len = tvb_length_remaining(encryptedData,0);
1354
1355         if (cryptgrm_len % 8) {
1356                 *error = "decryptionError: the length of the encrypted data is not a mutiple of 8 octets";
1357                 return NULL;
1358         }
1359
1360         cryptgrm = ep_tvb_memdup(encryptedData,0,-1);
1361
1362         cleartext = ep_alloc(cryptgrm_len);
1363
1364         err = gcry_cipher_open(&hd, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_CBC, 0);
1365         if (err != GPG_ERR_NO_ERROR) goto on_gcry_error;
1366
1367         err = gcry_cipher_setiv(hd, iv, 8);
1368         if (err != GPG_ERR_NO_ERROR) goto on_gcry_error;
1369
1370         err = gcry_cipher_setkey(hd,des_key,8);
1371         if (err != GPG_ERR_NO_ERROR) goto on_gcry_error;
1372
1373         err = gcry_cipher_decrypt(hd, cleartext, cryptgrm_len, cryptgrm, cryptgrm_len);
1374         if (err != GPG_ERR_NO_ERROR) goto on_gcry_error;
1375
1376         gcry_cipher_close(hd);
1377
1378         clear_tvb = tvb_new_child_real_data(encryptedData, cleartext, cryptgrm_len, cryptgrm_len);
1379
1380         return clear_tvb;
1381
1382 on_gcry_error:
1383         *error = (void*)gpg_strerror(err);
1384         if (hd) gcry_cipher_close(hd);
1385         return NULL;
1386 #else
1387         *error = "libgcrypt not present, cannot decrypt";
1388         return NULL;
1389 #endif
1390 }
1391
1392 static tvbuff_t*
1393 snmp_usm_priv_aes(snmp_usm_params_t* p _U_, tvbuff_t* encryptedData _U_, gchar const** error _U_)
1394 {
1395 #ifdef HAVE_LIBGCRYPT
1396         gcry_error_t err;
1397         gcry_cipher_hd_t hd = NULL;
1398
1399         guint8* cleartext;
1400         guint8* aes_key = p->user_assoc->user.privKey.data; /* first 16 bytes */
1401         guint8 iv[16];
1402         gint priv_len;
1403         gint cryptgrm_len;
1404         guint8* cryptgrm;
1405         tvbuff_t* clear_tvb;
1406
1407         priv_len = tvb_length_remaining(p->priv_tvb,0);
1408
1409         if (priv_len != 8)  {
1410                 *error = "decryptionError: msgPrivacyParameters length != 8";
1411                 return NULL;
1412         }
1413
1414         iv[0] = (p->boots & 0xff000000) >> 24;
1415         iv[1] = (p->boots & 0x00ff0000) >> 16;
1416         iv[2] = (p->boots & 0x0000ff00) >> 8;
1417         iv[3] = (p->boots & 0x000000ff);
1418         iv[4] = (p->time & 0xff000000) >> 24;
1419         iv[5] = (p->time & 0x00ff0000) >> 16;
1420         iv[6] = (p->time & 0x0000ff00) >> 8;
1421         iv[7] = (p->time & 0x000000ff);
1422         tvb_memcpy(p->priv_tvb,&(iv[8]),0,8);
1423
1424         cryptgrm_len = tvb_length_remaining(encryptedData,0);
1425         cryptgrm = ep_tvb_memdup(encryptedData,0,-1);
1426
1427         cleartext = ep_alloc(cryptgrm_len);
1428
1429         err = gcry_cipher_open(&hd, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CFB, 0);
1430         if (err != GPG_ERR_NO_ERROR) goto on_gcry_error;
1431
1432         err = gcry_cipher_setiv(hd, iv, 16);
1433         if (err != GPG_ERR_NO_ERROR) goto on_gcry_error;
1434
1435         err = gcry_cipher_setkey(hd,aes_key,16);
1436         if (err != GPG_ERR_NO_ERROR) goto on_gcry_error;
1437
1438         err = gcry_cipher_decrypt(hd, cleartext, cryptgrm_len, cryptgrm, cryptgrm_len);
1439         if (err != GPG_ERR_NO_ERROR) goto on_gcry_error;
1440
1441         gcry_cipher_close(hd);
1442
1443         clear_tvb = tvb_new_child_real_data(encryptedData, cleartext, cryptgrm_len, cryptgrm_len);
1444
1445         return clear_tvb;
1446
1447 on_gcry_error:
1448         *error = (void*)gpg_strerror(err);
1449         if (hd) gcry_cipher_close(hd);
1450         return NULL;
1451 #else
1452         *error = "libgcrypt not present, cannot decrypt";
1453         return NULL;
1454 #endif
1455 }
1456
1457
1458 gboolean
1459 check_ScopedPdu(tvbuff_t* tvb)
1460 {
1461         int offset;
1462         gint8 class;
1463         gboolean pc;
1464         gint32 tag;
1465         int hoffset, eoffset;
1466         guint32 len;
1467
1468         offset = get_ber_identifier(tvb, 0, &class, &pc, &tag);
1469         offset = get_ber_length(tvb, offset, NULL, NULL);
1470
1471         if ( ! (((class!=BER_CLASS_APP) && (class!=BER_CLASS_PRI) )
1472                         && ( (!pc) || (class!=BER_CLASS_UNI) || (tag!=BER_UNI_TAG_ENUMERATED) )
1473                         )) return FALSE;
1474
1475         if((tvb_get_guint8(tvb, offset)==0)&&(tvb_get_guint8(tvb, offset+1)==0))
1476                 return TRUE;
1477
1478         hoffset = offset;
1479
1480         offset = get_ber_identifier(tvb, offset, &class, &pc, &tag);
1481         offset = get_ber_length(tvb, offset, &len, NULL);
1482         eoffset = offset + len;
1483
1484         if (eoffset <= hoffset) return FALSE;
1485
1486         if ((class!=BER_CLASS_APP)&&(class!=BER_CLASS_PRI))
1487                 if( (class!=BER_CLASS_UNI)
1488                         ||((tag<BER_UNI_TAG_NumericString)&&(tag!=BER_UNI_TAG_OCTETSTRING)&&(tag!=BER_UNI_TAG_UTF8String)) )
1489                         return FALSE;
1490
1491         return TRUE;
1492
1493 }
1494
1495 #include "packet-snmp-fn.c"
1496
1497
1498 guint
1499 dissect_snmp_pdu(tvbuff_t *tvb, int offset, packet_info *pinfo,
1500                  proto_tree *tree, int proto, gint ett, gboolean is_tcp)
1501 {
1502
1503         guint length_remaining;
1504         gint8 class;
1505         gboolean pc, ind = 0;
1506         gint32 tag;
1507         guint32 len;
1508         guint message_length;
1509         int start_offset = offset;
1510         guint32 version = 0;
1511         tvbuff_t        *next_tvb;
1512
1513         proto_tree *snmp_tree = NULL;
1514         proto_item *item = NULL;
1515         asn1_ctx_t asn1_ctx;
1516         asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, TRUE, pinfo);
1517
1518
1519         usm_p.msg_tvb = tvb;
1520         usm_p.start_offset = tvb_offset_from_real_beginning(tvb);
1521         usm_p.engine_tvb = NULL;
1522         usm_p.user_tvb = NULL;
1523         usm_p.auth_item = NULL;
1524         usm_p.auth_tvb = NULL;
1525         usm_p.auth_offset = 0;
1526         usm_p.priv_tvb = NULL;
1527         usm_p.user_assoc = NULL;
1528         usm_p.authenticated = FALSE;
1529         usm_p.encrypted = FALSE;
1530         usm_p.boots = 0;
1531         usm_p.time = 0;
1532         usm_p.authOK = FALSE;
1533
1534         /*
1535          * This will throw an exception if we don't have any data left.
1536          * That's what we want.  (See "tcp_dissect_pdus()", which is
1537          * similar, but doesn't have to deal with ASN.1.
1538          * XXX - can we make "tcp_dissect_pdus()" provide enough
1539          * information to the "get_pdu_len" routine so that we could
1540          * have that routine deal with ASN.1, and just use
1541          * "tcp_dissect_pdus()"?)
1542          */
1543         length_remaining = tvb_ensure_length_remaining(tvb, offset);
1544
1545         /* NOTE: we have to parse the message piece by piece, since the
1546          * capture length may be less than the message length: a 'global'
1547          * parsing is likely to fail.
1548          */
1549
1550         /*
1551          * If this is SNMP-over-TCP, we might have to do reassembly
1552          * in order to read the "Sequence Of" header.
1553          */
1554         if (is_tcp && snmp_desegment && pinfo->can_desegment) {
1555                 /*
1556                  * This is TCP, and we should, and can, do reassembly.
1557                  *
1558                  * Is the "Sequence Of" header split across segment
1559                  * boundaries?  We require at least 6 bytes for the
1560                  * header, which allows for a 4-byte length (ASN.1
1561                  * BER).
1562                  */
1563                 if (length_remaining < 6) {
1564                         /*
1565                          * Yes.  Tell the TCP dissector where the data
1566                          * for this message starts in the data it handed
1567                          * us and that we need "some more data."  Don't tell
1568                          * it exactly how many bytes we need because if/when
1569                          * we ask for even more (after the header) that will
1570                          * break reassembly.
1571                          */
1572                         pinfo->desegment_offset = offset;
1573                         pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
1574
1575                         /*
1576                          * Return 0, which means "I didn't dissect anything
1577                          * because I don't have enough data - we need
1578                          * to desegment".
1579                          */
1580                         return 0;
1581                 }
1582         }
1583
1584         /*
1585          * OK, try to read the "Sequence Of" header; this gets the total
1586          * length of the SNMP message.
1587          */
1588         /* Set tree to 0 to not display internal BER fields if option used.*/
1589         offset = dissect_ber_identifier(pinfo, 0, tvb, offset, &class, &pc, &tag);
1590         /*Get the total octet length of the SNMP data*/
1591         offset = dissect_ber_length(pinfo, 0, tvb, offset, &len, &ind);
1592         message_length = len + 2;
1593
1594         /*Get the SNMP version data*/
1595         offset = dissect_ber_integer(FALSE, &asn1_ctx, 0, tvb, offset, -1, &version);
1596
1597
1598         /*
1599          * If this is SNMP-over-TCP, we might have to do reassembly
1600          * to get all of this message.
1601          */
1602         if (is_tcp && snmp_desegment && pinfo->can_desegment) {
1603                 /*
1604                  * Yes - is the message split across segment boundaries?
1605                  */
1606                 if (length_remaining < message_length) {
1607                         /*
1608                          * Yes.  Tell the TCP dissector where the data
1609                          * for this message starts in the data it handed
1610                          * us, and how many more bytes we need, and
1611                          * return.
1612                          */
1613                         pinfo->desegment_offset = start_offset;
1614                         pinfo->desegment_len =
1615                         message_length - length_remaining;
1616
1617                         /*
1618                          * Return 0, which means "I didn't dissect anything
1619                          * because I don't have enough data - we need
1620                          * to desegment".
1621                          */
1622                         return 0;
1623                 }
1624         }
1625
1626         next_tvb_init(&var_list);
1627
1628         col_set_str(pinfo->cinfo, COL_PROTOCOL,
1629             proto_get_protocol_short_name(find_protocol_by_id(proto)));
1630
1631         if (tree) {
1632                 item = proto_tree_add_item(tree, proto, tvb, start_offset,
1633                                            message_length, ENC_BIG_ENDIAN);
1634                 snmp_tree = proto_item_add_subtree(item, ett);
1635         }
1636
1637         switch (version) {
1638         case 0: /* v1 */
1639         case 1: /* v2c */
1640                 offset = dissect_snmp_Message(FALSE , tvb, start_offset, &asn1_ctx, snmp_tree, -1);
1641                 break;
1642         case 2: /* v2u */
1643                 offset = dissect_snmp_Messagev2u(FALSE , tvb, start_offset, &asn1_ctx, snmp_tree, -1);
1644                 break;
1645                         /* v3 */
1646         case 3:
1647                 offset = dissect_snmp_SNMPv3Message(FALSE , tvb, start_offset, &asn1_ctx, snmp_tree, -1);
1648                 break;
1649         default:
1650                 /*
1651                  * Return the length remaining in the tvbuff, so
1652                  * if this is SNMP-over-TCP, our caller thinks there's
1653                  * nothing left to dissect.
1654                  */
1655                 proto_tree_add_text(snmp_tree, tvb, offset, -1,"Unknown version");
1656                 return length_remaining;
1657                 break;
1658         }
1659
1660         /* There may be appended data after the SNMP data, so treat as raw
1661          * data which needs to be dissected in case of UDP as UDP is PDU oriented.
1662          */
1663         if((!is_tcp) && (length_remaining > (guint)offset)) {
1664                 next_tvb = tvb_new_subset_remaining(tvb, offset);
1665                 call_dissector(data_handle, next_tvb, pinfo, tree);
1666         } else {
1667                 next_tvb_call(&var_list, pinfo, tree, NULL, data_handle);
1668         }
1669
1670         return offset;
1671 }
1672
1673 static gint
1674 dissect_snmp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1675 {
1676         conversation_t  *conversation;
1677         int offset;
1678         gint8 tmp_class;
1679         gboolean tmp_pc;
1680         gint32 tmp_tag;
1681         guint32 tmp_length;
1682         gboolean tmp_ind;
1683
1684         /*
1685          * See if this looks like SNMP or not. if not, return 0 so
1686          * wireshark can try som other dissector instead.
1687          */
1688         /* All SNMP packets are BER encoded and consist of a SEQUENCE
1689          * that spans the entire PDU. The first item is an INTEGER that
1690          * has the values 0-2 (version 1-3).
1691          * if not it is not snmp.
1692          */
1693         /* SNMP starts with a SEQUENCE */
1694         offset = get_ber_identifier(tvb, 0, &tmp_class, &tmp_pc, &tmp_tag);
1695         if((tmp_class!=BER_CLASS_UNI)||(tmp_tag!=BER_UNI_TAG_SEQUENCE)) {
1696                 return 0;
1697         }
1698         /* then comes a length which spans the rest of the tvb */
1699         offset = get_ber_length(tvb, offset, &tmp_length, &tmp_ind);
1700         /* if(tmp_length!=(guint32)tvb_reported_length_remaining(tvb, offset)) {
1701          * Losen the heuristic a bit to handle the case where data has intentionally
1702          * been added after the snmp PDU ( UDP case)
1703          */
1704         if ( pinfo->ptype == PT_UDP ) {
1705                 if(tmp_length>(guint32)tvb_reported_length_remaining(tvb, offset)) {
1706                         return 0;
1707                 }
1708         }else{
1709                 if(tmp_length!=(guint32)tvb_reported_length_remaining(tvb, offset)) {
1710                         return 0;
1711                 }
1712         }
1713         /* then comes an INTEGER (version)*/
1714         get_ber_identifier(tvb, offset, &tmp_class, &tmp_pc, &tmp_tag);
1715         if((tmp_class!=BER_CLASS_UNI)||(tmp_tag!=BER_UNI_TAG_INTEGER)) {
1716                 return 0;
1717         }
1718         /* do we need to test that version is 0 - 2 (version1-3) ? */
1719
1720
1721         /*
1722          * The first SNMP packet goes to the SNMP port; the second one
1723          * may come from some *other* port, but goes back to the same
1724          * IP address and port as the ones from which the first packet
1725          * came; all subsequent packets presumably go between those two
1726          * IP addresses and ports.
1727          *
1728          * If this packet went to the SNMP port, we check to see if
1729          * there's already a conversation with one address/port pair
1730          * matching the source IP address and port of this packet,
1731          * the other address matching the destination IP address of this
1732          * packet, and any destination port.
1733          *
1734          * If not, we create one, with its address 1/port 1 pair being
1735          * the source address/port of this packet, its address 2 being
1736          * the destination address of this packet, and its port 2 being
1737          * wildcarded, and give it the SNMP dissector as a dissector.
1738          */
1739         if (pinfo->destport == UDP_PORT_SNMP) {
1740           conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst, PT_UDP,
1741                                            pinfo->srcport, 0, NO_PORT_B);
1742           if( (conversation == NULL) || (conversation->dissector_handle!=snmp_handle) ) {
1743             conversation = conversation_new(pinfo->fd->num, &pinfo->src, &pinfo->dst, PT_UDP,
1744                                             pinfo->srcport, 0, NO_PORT2);
1745             conversation_set_dissector(conversation, snmp_handle);
1746           }
1747         }
1748
1749         return dissect_snmp_pdu(tvb, 0, pinfo, tree, proto_snmp, ett_snmp, FALSE);
1750 }
1751
1752 static void
1753 dissect_snmp_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1754 {
1755         int offset = 0;
1756         guint message_len;
1757
1758         while (tvb_reported_length_remaining(tvb, offset) > 0) {
1759                 message_len = dissect_snmp_pdu(tvb, 0, pinfo, tree,
1760                                                proto_snmp, ett_snmp, TRUE);
1761                 if (message_len == 0) {
1762                         /*
1763                          * We don't have all the data for that message,
1764                          * so we need to do desegmentation;
1765                          * "dissect_snmp_pdu()" has set that up.
1766                          */
1767                         break;
1768                 }
1769                 offset += message_len;
1770         }
1771 }
1772
1773 static void
1774 dissect_smux(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1775 {
1776         proto_tree *smux_tree = NULL;
1777         proto_item *item = NULL;
1778
1779         next_tvb_init(&var_list);
1780
1781         col_set_str(pinfo->cinfo, COL_PROTOCOL, "SMUX");
1782
1783         if (tree) {
1784                 item = proto_tree_add_item(tree, proto_smux, tvb, 0, -1, ENC_BIG_ENDIAN);
1785                 smux_tree = proto_item_add_subtree(item, ett_smux);
1786         }
1787
1788         dissect_SMUX_PDUs_PDU(tvb, pinfo, smux_tree);
1789 }
1790
1791
1792 /*
1793   MD5 Password to Key Algorithm
1794   from RFC 3414 A.2.1
1795 */
1796 static void
1797 snmp_usm_password_to_key_md5(const guint8 *password, guint passwordlen,
1798                              const guint8 *engineID, guint   engineLength,
1799                              guint8 *key)
1800 {
1801         md5_state_t     MD;
1802         guint8     *cp, password_buf[64];
1803         guint32      password_index = 0;
1804         guint32      count = 0, i;
1805         guint8          key1[16];
1806         md5_init(&MD);   /* initialize MD5 */
1807
1808         /**********************************************/
1809         /* Use while loop until we've done 1 Megabyte */
1810         /**********************************************/
1811         while (count < 1048576) {
1812                 cp = password_buf;
1813                 for (i = 0; i < 64; i++) {
1814                         /*************************************************/
1815                         /* Take the next octet of the password, wrapping */
1816                         /* to the beginning of the password as necessary.*/
1817                         /*************************************************/
1818                         *cp++ = password[password_index++ % passwordlen];
1819                 }
1820                 md5_append(&MD, password_buf, 64);
1821                 count += 64;
1822         }
1823         md5_finish(&MD, key1);          /* tell MD5 we're done */
1824
1825         /*****************************************************/
1826         /* Now localize the key with the engineID and pass   */
1827         /* through MD5 to produce final key                  */
1828         /* We ignore invalid engineLengths here. More strict */
1829         /* checking is done in snmp_users_update_cb.         */
1830         /*****************************************************/
1831
1832         md5_init(&MD);
1833         md5_append(&MD, key1, 16);
1834         md5_append(&MD, engineID, engineLength);
1835         md5_append(&MD, key1, 16);
1836         md5_finish(&MD, key);
1837
1838         return;
1839 }
1840
1841
1842
1843
1844 /*
1845    SHA1 Password to Key Algorithm COPIED from RFC 3414 A.2.2
1846  */
1847
1848 static void
1849 snmp_usm_password_to_key_sha1(const guint8 *password, guint passwordlen,
1850                               const guint8 *engineID, guint engineLength,
1851                               guint8 *key)
1852 {
1853         sha1_context     SH;
1854         guint8     *cp, password_buf[64];
1855         guint32      password_index = 0;
1856         guint32      count = 0, i;
1857
1858         sha1_starts(&SH);   /* initialize SHA */
1859
1860         /**********************************************/
1861         /* Use while loop until we've done 1 Megabyte */
1862         /**********************************************/
1863         while (count < 1048576) {
1864                 cp = password_buf;
1865                 for (i = 0; i < 64; i++) {
1866                         /*************************************************/
1867                         /* Take the next octet of the password, wrapping */
1868                         /* to the beginning of the password as necessary.*/
1869                         /*************************************************/
1870                         *cp++ = password[password_index++ % passwordlen];
1871                 }
1872                 sha1_update (&SH, password_buf, 64);
1873                 count += 64;
1874         }
1875         sha1_finish(&SH, key);
1876
1877         /*****************************************************/
1878         /* Now localize the key with the engineID and pass   */
1879         /* through SHA to produce final key                  */
1880         /* We ignore invalid engineLengths here. More strict */
1881         /* checking is done in snmp_users_update_cb.         */
1882         /*****************************************************/
1883
1884         sha1_starts(&SH);
1885         sha1_update(&SH, key, 20);
1886         sha1_update(&SH, engineID, engineLength);
1887         sha1_update(&SH, key, 20);
1888         sha1_finish(&SH, key);
1889         return;
1890  }
1891
1892
1893 static void
1894 process_prefs(void)
1895 {
1896 }
1897
1898 static void*
1899 snmp_users_copy_cb(void* dest, const void* orig, size_t len _U_)
1900 {
1901         const snmp_ue_assoc_t* o = orig;
1902         snmp_ue_assoc_t* d = dest;
1903
1904         d->auth_model = o->auth_model;
1905         d->user.authModel = auth_models[o->auth_model];
1906
1907         d->priv_proto = o->priv_proto;
1908         d->user.privProtocol = priv_protos[o->priv_proto];
1909
1910         d->user.userName.data = g_memdup(o->user.userName.data,o->user.userName.len);
1911         d->user.userName.len = o->user.userName.len;
1912
1913         d->user.authPassword.data = o->user.authPassword.data ? g_memdup(o->user.authPassword.data,o->user.authPassword.len) : NULL;
1914         d->user.authPassword.len = o->user.authPassword.len;
1915
1916         d->user.privPassword.data = o->user.privPassword.data ? g_memdup(o->user.privPassword.data,o->user.privPassword.len) : NULL;
1917         d->user.privPassword.len = o->user.privPassword.len;
1918
1919         d->engine.len = o->engine.len;
1920         if (o->engine.data) {
1921                 d->engine.data = g_memdup(o->engine.data,o->engine.len);
1922         }
1923
1924         d->user.authKey.data = o->user.authKey.data ? g_memdup(o->user.authKey.data,o->user.authKey.len) : NULL;
1925         d->user.authKey.len = o->user.authKey.len;
1926
1927         d->user.privKey.data = o->user.privKey.data ? g_memdup(o->user.privKey.data,o->user.privKey.len) : NULL;
1928         d->user.privKey.len = o->user.privKey.len;
1929
1930         return d;
1931 }
1932
1933 static void
1934 snmp_users_free_cb(void* p)
1935 {
1936         snmp_ue_assoc_t* ue = p;
1937         g_free(ue->user.userName.data);
1938         g_free(ue->user.authPassword.data);
1939         g_free(ue->user.privPassword.data);
1940         g_free(ue->user.authKey.data);
1941         g_free(ue->user.privKey.data);
1942         g_free(ue->engine.data);
1943 }
1944
1945 static void
1946 snmp_users_update_cb(void* p _U_, const char** err)
1947 {
1948         snmp_ue_assoc_t* ue = p;
1949         GString* es = g_string_new("");
1950         unsigned i;
1951
1952         *err = NULL;
1953
1954         if (num_ueas == 0)
1955                 /* Nothing to update */
1956                 return;
1957
1958         if (! ue->user.userName.len)
1959                 g_string_append_printf(es,"no userName\n");
1960
1961         for (i=0; i<num_ueas-1; i++) {
1962                 snmp_ue_assoc_t* u = &(ueas[i]);
1963
1964                 /* RFC 3411 section 5 */
1965                 if ((u->engine.len > 0) && (u->engine.len < 5 || u->engine.len > 32)) {
1966                         g_string_append_printf(es, "Invalid engineId length (%u). Must be between 5 and 32 (10 and 64 hex digits)\n", u->engine.len);
1967                 }
1968
1969
1970                 if ( u->user.userName.len == ue->user.userName.len
1971                         && u->engine.len == ue->engine.len ) {
1972
1973                         if (u->engine.len > 0 && memcmp( u->engine.data,   ue->engine.data,  u->engine.len ) == 0) {
1974                                 if ( memcmp( u->user.userName.data, ue->user.userName.data, ue->user.userName.len ) == 0 ) {
1975                                         /* XXX: make a string for the engineId */
1976                                         g_string_append_printf(es,"Duplicate key (userName='%s')\n",ue->user.userName.data);
1977                                 }
1978                         }
1979
1980                         if (u->engine.len == 0) {
1981                                 if ( memcmp( u->user.userName.data, ue->user.userName.data, ue->user.userName.len ) == 0 ) {
1982                                         g_string_append_printf(es,"Duplicate key (userName='%s' engineId=NONE)\n",ue->user.userName.data);
1983                                 }
1984                         }
1985                 }
1986         }
1987
1988         if (es->len) {
1989                 g_string_truncate(es,es->len-1);
1990                 *err = ep_strdup(es->str);
1991         }
1992
1993         g_string_free(es,TRUE);
1994
1995         return;
1996 }
1997
1998
1999 UAT_LSTRING_CB_DEF(snmp_users,userName,snmp_ue_assoc_t,user.userName.data,user.userName.len)
2000 UAT_LSTRING_CB_DEF(snmp_users,authPassword,snmp_ue_assoc_t,user.authPassword.data,user.authPassword.len)
2001 UAT_LSTRING_CB_DEF(snmp_users,privPassword,snmp_ue_assoc_t,user.privPassword.data,user.privPassword.len)
2002 UAT_BUFFER_CB_DEF(snmp_users,engine_id,snmp_ue_assoc_t,engine.data,engine.len)
2003 UAT_VS_DEF(snmp_users,auth_model,snmp_ue_assoc_t,0,"MD5")
2004 UAT_VS_DEF(snmp_users,priv_proto,snmp_ue_assoc_t,0,"DES")
2005
2006 static void *
2007 snmp_specific_trap_copy_cb(void *dest, const void *orig, size_t len _U_)
2008 {
2009         snmp_st_assoc_t *u = dest;
2010         const snmp_st_assoc_t *o = orig;
2011
2012         u->enterprise = g_strdup(o->enterprise);
2013         u->trap = o->trap;
2014         u->desc = g_strdup(o->desc);
2015
2016         return dest;
2017 }
2018
2019 static void
2020 snmp_specific_trap_free_cb(void *r)
2021 {
2022         snmp_st_assoc_t *u = r;
2023
2024         g_free(u->enterprise);
2025         g_free(u->desc);
2026 }
2027
2028 UAT_CSTRING_CB_DEF(specific_traps, enterprise, snmp_st_assoc_t)
2029 UAT_DEC_CB_DEF(specific_traps, trap, snmp_st_assoc_t)
2030 UAT_CSTRING_CB_DEF(specific_traps, desc, snmp_st_assoc_t)
2031
2032         /*--- proto_register_snmp -------------------------------------------*/
2033 void proto_register_snmp(void) {
2034   /* List of fields */
2035   static hf_register_info hf[] = {
2036                 { &hf_snmp_v3_flags_auth,
2037                 { "Authenticated", "snmp.v3.flags.auth", FT_BOOLEAN, 8,
2038                     TFS(&tfs_set_notset), TH_AUTH, NULL, HFILL }},
2039                 { &hf_snmp_v3_flags_crypt,
2040                 { "Encrypted", "snmp.v3.flags.crypt", FT_BOOLEAN, 8,
2041                     TFS(&tfs_set_notset), TH_CRYPT, NULL, HFILL }},
2042                 { &hf_snmp_v3_flags_report,
2043                 { "Reportable", "snmp.v3.flags.report", FT_BOOLEAN, 8,
2044                     TFS(&tfs_set_notset), TH_REPORT, NULL, HFILL }},
2045                 { &hf_snmp_engineid_conform, {
2046                     "Engine ID Conformance", "snmp.engineid.conform", FT_BOOLEAN, 8,
2047                     TFS(&tfs_snmp_engineid_conform), F_SNMP_ENGINEID_CONFORM, "Engine ID RFC3411 Conformance", HFILL }},
2048                 { &hf_snmp_engineid_enterprise, {
2049                     "Engine Enterprise ID", "snmp.engineid.enterprise", FT_UINT32, BASE_DEC|BASE_EXT_STRING,
2050                     &sminmpec_values_ext, 0, NULL, HFILL }},
2051                 { &hf_snmp_engineid_format, {
2052                     "Engine ID Format", "snmp.engineid.format", FT_UINT8, BASE_DEC,
2053                     VALS(snmp_engineid_format_vals), 0, NULL, HFILL }},
2054                 { &hf_snmp_engineid_ipv4, {
2055                     "Engine ID Data: IPv4 address", "snmp.engineid.ipv4", FT_IPv4, BASE_NONE,
2056                     NULL, 0, NULL, HFILL }},
2057                 { &hf_snmp_engineid_ipv6, {
2058                     "Engine ID Data: IPv6 address", "snmp.engineid.ipv6", FT_IPv6, BASE_NONE,
2059                     NULL, 0, NULL, HFILL }},
2060                 { &hf_snmp_engineid_cisco_type, {
2061                     "Engine ID Data: Cisco type", "snmp.engineid.cisco.type", FT_UINT8, BASE_NONE,
2062                     VALS(snmp_engineid_cisco_type_vals), 0, NULL, HFILL }},
2063                 { &hf_snmp_engineid_mac, {
2064                     "Engine ID Data: MAC address", "snmp.engineid.mac", FT_ETHER, BASE_NONE,
2065                     NULL, 0, NULL, HFILL }},
2066                 { &hf_snmp_engineid_text, {
2067                     "Engine ID Data: Text", "snmp.engineid.text", FT_STRING, BASE_NONE,
2068                     NULL, 0, NULL, HFILL }},
2069                 { &hf_snmp_engineid_time, {
2070                     "Engine ID Data: Creation Time", "snmp.engineid.time", FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL,
2071                     NULL, 0, NULL, HFILL }},
2072                 { &hf_snmp_engineid_data, {
2073                     "Engine ID Data", "snmp.engineid.data", FT_BYTES, BASE_NONE,
2074                     NULL, 0, NULL, HFILL }},
2075                 { &hf_snmp_msgAuthentication, {
2076                     "Authentication", "snmp.v3.auth", FT_BOOLEAN, BASE_NONE,
2077                     TFS(&auth_flags), 0, NULL, HFILL }},
2078                 { &hf_snmp_decryptedPDU, {
2079                     "Decrypted ScopedPDU", "snmp.decrypted_pdu", FT_BYTES, BASE_NONE,
2080                     NULL, 0, "Decrypted PDU", HFILL }},
2081                 { &hf_snmp_noSuchObject, {
2082                     "noSuchObject", "snmp.noSuchObject", FT_NONE, BASE_NONE,
2083                     NULL, 0, NULL, HFILL }},
2084                 { &hf_snmp_noSuchInstance, {
2085                     "noSuchInstance", "snmp.noSuchInstance", FT_NONE, BASE_NONE,
2086                     NULL, 0, NULL, HFILL }},
2087                 { &hf_snmp_endOfMibView, {
2088                     "endOfMibView", "snmp.endOfMibView", FT_NONE, BASE_NONE,
2089                     NULL, 0, NULL, HFILL }},
2090                 { &hf_snmp_unSpecified, {
2091                     "unSpecified", "snmp.unSpecified", FT_NONE, BASE_NONE,
2092                     NULL, 0, NULL, HFILL }},
2093
2094                 { &hf_snmp_integer32_value, {
2095                     "Value (Integer32)", "snmp.value.int", FT_INT64, BASE_DEC,
2096                     NULL, 0, NULL, HFILL }},
2097                 { &hf_snmp_octetstring_value, {
2098                     "Value (OctetString)", "snmp.value.octets", FT_BYTES, BASE_NONE,
2099                     NULL, 0, NULL, HFILL }},
2100                 { &hf_snmp_oid_value, {
2101                     "Value (OID)", "snmp.value.oid", FT_OID, BASE_NONE,
2102                     NULL, 0, NULL, HFILL }},
2103                 { &hf_snmp_null_value, {
2104                     "Value (Null)", "snmp.value.null", FT_NONE, BASE_NONE,
2105                     NULL, 0, NULL, HFILL }},
2106                 { &hf_snmp_ipv4_value, {
2107                     "Value (IpAddress)", "snmp.value.ipv4", FT_IPv4, BASE_NONE,
2108                     NULL, 0, NULL, HFILL }},
2109                 { &hf_snmp_ipv6_value, {
2110                     "Value (IpAddress)", "snmp.value.ipv6", FT_IPv6, BASE_NONE,
2111                     NULL, 0, NULL, HFILL }},
2112                 { &hf_snmp_anyaddress_value, {
2113                     "Value (IpAddress)", "snmp.value.addr", FT_BYTES, BASE_NONE,
2114                     NULL, 0, NULL, HFILL }},
2115                 { &hf_snmp_unsigned32_value, {
2116                     "Value (Unsigned32)", "snmp.value.u32", FT_INT64, BASE_DEC,
2117                     NULL, 0, NULL, HFILL }},
2118                 { &hf_snmp_gauge32_value, {
2119                     "Value (Gauge32)", "snmp.value.g32", FT_INT64, BASE_DEC,
2120                     NULL, 0, NULL, HFILL }},
2121                 { &hf_snmp_unknown_value, {
2122                     "Value (Unknown)", "snmp.value.unk", FT_BYTES, BASE_NONE,
2123                     NULL, 0, NULL, HFILL }},
2124                 { &hf_snmp_counter_value, {
2125                     "Value (Counter32)", "snmp.value.counter", FT_UINT64, BASE_DEC,
2126                     NULL, 0, NULL, HFILL }},
2127                 { &hf_snmp_big_counter_value, {
2128                     "Value (Counter64)", "snmp.value.counter", FT_UINT64, BASE_DEC,
2129                     NULL, 0, NULL, HFILL }},
2130                 { &hf_snmp_nsap_value, {
2131                     "Value (NSAP)", "snmp.value.nsap", FT_UINT64, BASE_DEC,
2132                     NULL, 0, NULL, HFILL }},
2133                 { &hf_snmp_timeticks_value, {
2134                     "Value (Timeticks)", "snmp.value.timeticks", FT_UINT64, BASE_DEC,
2135                     NULL, 0, NULL, HFILL }},
2136                 { &hf_snmp_opaque_value, {
2137                     "Value (Opaque)", "snmp.value.opaque", FT_BYTES, BASE_NONE,
2138                     NULL, 0, NULL, HFILL }},
2139                 { &hf_snmp_objectname, {
2140                     "Object Name", "snmp.name", FT_OID, BASE_NONE,
2141                     NULL, 0, NULL, HFILL }},
2142                 { &hf_snmp_scalar_instance_index, {
2143                     "Scalar Instance Index", "snmp.name.index", FT_UINT64, BASE_DEC,
2144                     NULL, 0, NULL, HFILL }},
2145
2146
2147 #include "packet-snmp-hfarr.c"
2148   };
2149
2150   /* List of subtrees */
2151   static gint *ett[] = {
2152           &ett_snmp,
2153           &ett_engineid,
2154           &ett_msgFlags,
2155           &ett_encryptedPDU,
2156           &ett_decrypted,
2157           &ett_authParameters,
2158           &ett_internet,
2159           &ett_varbind,
2160           &ett_name,
2161           &ett_value,
2162           &ett_decoding_error,
2163 #include "packet-snmp-ettarr.c"
2164   };
2165   module_t *snmp_module;
2166
2167   static uat_field_t users_fields[] = {
2168           UAT_FLD_BUFFER(snmp_users,engine_id,"Engine ID","Engine-id for this entry (empty = any)"),
2169           UAT_FLD_LSTRING(snmp_users,userName,"Username","The username"),
2170           UAT_FLD_VS(snmp_users,auth_model,"Authentication model",auth_types,"Algorithm to be used for authentication."),
2171           UAT_FLD_LSTRING(snmp_users,authPassword,"Password","The password used for authenticating packets for this entry"),
2172           UAT_FLD_VS(snmp_users,priv_proto,"Privacy protocol",priv_types,"Algorithm to be used for privacy."),
2173           UAT_FLD_LSTRING(snmp_users,privPassword,"Privacy password","The password used for encrypting packets for this entry"),
2174           UAT_END_FIELDS
2175   };
2176
2177   uat_t *assocs_uat = uat_new("SNMP Users",
2178                               sizeof(snmp_ue_assoc_t),
2179                               "snmp_users",
2180                               TRUE,
2181                               (void*)&ueas,
2182                               &num_ueas,
2183                               UAT_CAT_CRYPTO,
2184                               "ChSNMPUsersSection",
2185                               snmp_users_copy_cb,
2186                               snmp_users_update_cb,
2187                               snmp_users_free_cb,
2188                               renew_ue_cache,
2189                               users_fields);
2190
2191   static uat_field_t specific_traps_flds[] = {
2192     UAT_FLD_CSTRING(specific_traps,enterprise,"Enterprise OID","Enterprise Object Identifier"),
2193     UAT_FLD_DEC(specific_traps,trap,"Trap Id","The specific-trap value"),
2194     UAT_FLD_CSTRING(specific_traps,desc,"Description","Trap type description"),
2195     UAT_END_FIELDS
2196   };
2197
2198   uat_t* specific_traps_uat = uat_new("SNMP Enterprise Specific Trap Types",
2199                                       sizeof(snmp_st_assoc_t),
2200                                       "snmp_specific_traps",
2201                                       TRUE,
2202                                       (void*) &specific_traps,
2203                                       &num_specific_traps,
2204                                       UAT_CAT_GENERAL,
2205                                       "ChSNMPEnterpriseSpecificTrapTypes",
2206                                       snmp_specific_trap_copy_cb,
2207                                       NULL,
2208                                       snmp_specific_trap_free_cb,
2209                                                                           NULL,
2210                                       specific_traps_flds);
2211
2212   /* Register protocol */
2213   proto_snmp = proto_register_protocol(PNAME, PSNAME, PFNAME);
2214   new_register_dissector("snmp", dissect_snmp, proto_snmp);
2215
2216   /* Register fields and subtrees */
2217   proto_register_field_array(proto_snmp, hf, array_length(hf));
2218   proto_register_subtree_array(ett, array_length(ett));
2219
2220
2221   /* Register configuration preferences */
2222   snmp_module = prefs_register_protocol(proto_snmp, process_prefs);
2223   prefs_register_bool_preference(snmp_module, "display_oid",
2224                         "Show SNMP OID in info column",
2225                         "Whether the SNMP OID should be shown in the info column",
2226                         &display_oid);
2227
2228   prefs_register_obsolete_preference(snmp_module, "mib_modules");
2229   prefs_register_obsolete_preference(snmp_module, "users_file");
2230
2231   prefs_register_bool_preference(snmp_module, "desegment",
2232                         "Reassemble SNMP-over-TCP messages\nspanning multiple TCP segments",
2233                         "Whether the SNMP dissector should reassemble messages spanning multiple TCP segments."
2234                         " To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
2235                         &snmp_desegment);
2236
2237   prefs_register_bool_preference(snmp_module, "var_in_tree",
2238                         "Display dissected variables inside SNMP tree",
2239                         "ON - display dissected variables inside SNMP tree, OFF - display dissected variables in root tree after SNMP",
2240                         &snmp_var_in_tree);
2241
2242   prefs_register_uat_preference(snmp_module, "users_table",
2243                                 "Users Table",
2244                                 "Table of engine-user associations used for authentication and decryption",
2245                                 assocs_uat);
2246
2247   prefs_register_uat_preference(snmp_module, "specific_traps_table",
2248                                 "Enterprise Specific Trap Types",
2249                                 "Table of enterprise specific-trap type descriptions",
2250                                 specific_traps_uat);
2251
2252 #ifdef HAVE_LIBSMI
2253   prefs_register_static_text_preference(snmp_module, "info_mibs",
2254                                         "MIB settings can be changed in the Name Resolution preferences",
2255                                         "MIB settings can be changed in the Name Resolution preferences");
2256 #endif
2257
2258   value_sub_dissectors_table = register_dissector_table("snmp.variable_oid","SNMP Variable OID", FT_STRING, BASE_NONE);
2259
2260   register_init_routine(renew_ue_cache);
2261
2262   register_ber_syntax_dissector("SNMP", proto_snmp, dissect_snmp_tcp);
2263 }
2264
2265
2266 /*--- proto_reg_handoff_snmp ---------------------------------------*/
2267 void proto_reg_handoff_snmp(void) {
2268         dissector_handle_t snmp_tcp_handle;
2269
2270         snmp_handle = find_dissector("snmp");
2271
2272         dissector_add_uint("udp.port", UDP_PORT_SNMP, snmp_handle);
2273         dissector_add_uint("udp.port", UDP_PORT_SNMP_TRAP, snmp_handle);
2274         dissector_add_uint("udp.port", UDP_PORT_SNMP_PATROL, snmp_handle);
2275         dissector_add_uint("ethertype", ETHERTYPE_SNMP, snmp_handle);
2276         dissector_add_uint("ipx.socket", IPX_SOCKET_SNMP_AGENT, snmp_handle);
2277         dissector_add_uint("ipx.socket", IPX_SOCKET_SNMP_SINK, snmp_handle);
2278         dissector_add_uint("hpext.dxsap", HPEXT_SNMP, snmp_handle);
2279
2280         snmp_tcp_handle = create_dissector_handle(dissect_snmp_tcp, proto_snmp);
2281         dissector_add_uint("tcp.port", TCP_PORT_SNMP, snmp_tcp_handle);
2282         dissector_add_uint("tcp.port", TCP_PORT_SNMP_TRAP, snmp_tcp_handle);
2283
2284         data_handle = find_dissector("data");
2285
2286         /*
2287          * Process preference settings.
2288          *
2289          * We can't do this in the register routine, as preferences aren't
2290          * read until all dissector register routines have been called (so
2291          * that all dissector preferences have been registered).
2292          */
2293         process_prefs();
2294
2295 }
2296
2297 void
2298 proto_register_smux(void)
2299 {
2300         static hf_register_info hf[] = {
2301                 { &hf_smux_version,
2302                 { "Version", "smux.version", FT_UINT8, BASE_DEC, NULL,
2303                     0x0, NULL, HFILL }},
2304                 { &hf_smux_pdutype,
2305                 { "PDU type", "smux.pdutype", FT_UINT8, BASE_DEC, VALS(smux_types),
2306                     0x0, NULL, HFILL }},
2307         };
2308         static gint *ett[] = {
2309                 &ett_smux,
2310         };
2311
2312         proto_smux = proto_register_protocol("SNMP Multiplex Protocol",
2313             "SMUX", "smux");
2314         proto_register_field_array(proto_smux, hf, array_length(hf));
2315         proto_register_subtree_array(ett, array_length(ett));
2316
2317 }
2318
2319 void
2320 proto_reg_handoff_smux(void)
2321 {
2322         dissector_handle_t smux_handle;
2323
2324         smux_handle = create_dissector_handle(dissect_smux, proto_smux);
2325         dissector_add_uint("tcp.port", TCP_PORT_SMUX, smux_handle);
2326 }