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