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