Apparently bit 0x80 of a SAMR domain handle is the permission to look
[metze/wireshark/wip.git] / packet-cops.c
1 /* packet-cops.c
2  * Routines for the COPS (Common Open Policy Service) protocol dissection
3  * RFC2748 & COPS-PR extension RFC3084
4  *
5  * Copyright 2000, Heikki Vatiainen <hessu@cs.tut.fi>
6  *
7  * $Id: packet-cops.c,v 1.33 2003/02/21 00:11:31 guy Exp $
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
11  * Copyright 1998 Gerald Combs
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <ctype.h>
36
37 #include <glib.h>
38 #include <epan/packet.h>
39 #include "packet-ipv6.h"
40 #include "packet-tcp.h"
41
42 #include "asn1.h"
43 #include "format-oid.h"
44 #include "prefs.h"
45
46 #define TCP_PORT_COPS 3288
47
48 /* Variable to hold the tcp port preference */
49 static guint global_cops_tcp_port = TCP_PORT_COPS;
50
51 /* desegmentation of COPS */
52 static gboolean cops_desegment = TRUE;
53
54 /* Variable to allow for proper deletion of dissector registration
55  * when the user changes port from the gui
56  */
57
58 static guint cops_tcp_port = 0;
59
60 static gchar *last_decoded_prid=NULL;
61
62 #define COPS_OBJECT_HDR_SIZE 4
63
64 /* Null string of type "guchar[]". */
65 static const guchar nullstring[] = "";
66
67 #define SAFE_STRING(s)  (((s) != NULL) ? (s) : nullstring)
68
69 /* COPS PR Tags */
70
71 #define COPS_IPA    0           /* IP Address */
72 #define COPS_U32    2           /* Unsigned 32*/
73 #define COPS_TIT    3           /* TimeTicks */
74 #define COPS_OPQ    4           /* Opaque */
75 #define COPS_I64    10          /* Integer64 */
76 #define COPS_U64    11          /* Uinteger64 */
77
78 /* COPS PR Types */
79
80 #define COPS_NULL                0
81 #define COPS_INTEGER             1    /* l  */
82 #define COPS_OCTETSTR            2    /* c  */
83 #define COPS_OBJECTID            3    /* ul */
84 #define COPS_IPADDR              4    /* uc */
85 #define COPS_UNSIGNED32          5    /* ul */
86 #define COPS_TIMETICKS           7    /* ul */
87 #define COPS_OPAQUE              8    /* c  */
88 #define COPS_INTEGER64           10   /* ll */
89 #define COPS_UNSIGNED64          11   /* ull  */
90
91
92 typedef struct _COPS_CNV COPS_CNV;
93
94 struct _COPS_CNV
95 {
96   guint class;
97   guint tag;
98   gint  syntax;
99   gchar *name;
100 };
101
102 static COPS_CNV CopsCnv [] =
103 {
104   {ASN1_UNI, ASN1_NUL, COPS_NULL,      "NULL"},
105   {ASN1_UNI, ASN1_INT, COPS_INTEGER,   "INTEGER"},
106   {ASN1_UNI, ASN1_OTS, COPS_OCTETSTR,  "OCTET STRING"},
107   {ASN1_UNI, ASN1_OJI, COPS_OBJECTID,  "OBJECTID"},
108   {ASN1_APL, COPS_IPA, COPS_IPADDR,    "IPADDR"},
109   {ASN1_APL, COPS_U32, COPS_UNSIGNED32,"UNSIGNED32"},
110   {ASN1_APL, COPS_TIT, COPS_TIMETICKS, "TIMETICKS"},
111   {ASN1_APL, COPS_OPQ, COPS_OPAQUE,    "OPAQUE"},
112   {ASN1_APL, COPS_I64, COPS_INTEGER64, "INTEGER64"},
113   {ASN1_APL, COPS_U64, COPS_UNSIGNED64, "UNSIGNED64"},
114   {0,       0,         -1,                  NULL}
115 };
116
117 static gchar *
118 cops_tag_cls2syntax ( guint tag, guint cls, gushort *syntax)
119 {
120     COPS_CNV *cnv;
121
122     cnv = CopsCnv;
123     while (cnv->syntax != -1)
124     {
125         if (cnv->tag == tag && cnv->class == cls)
126         {
127             *syntax = cnv->syntax;
128             return cnv->name;
129         }
130         cnv++;
131     }
132     return NULL;
133 }
134
135 static const value_string cops_flags_vals[] = {
136         { 0x00,          "None" },
137         { 0x01,          "Solicited Message Flag Bit" },
138         { 0, NULL },
139 };
140
141 /* The different COPS message types */
142 enum cops_op_code {
143         COPS_NO_MSG,          /* Not a COPS Message type     */
144
145         COPS_MSG_REQ,         /* Request (REQ)               */
146         COPS_MSG_DEC,         /* Decision (DEC)              */
147         COPS_MSG_RPT,         /* Report State (RPT)          */
148         COPS_MSG_DRQ,         /* Delete Request State (DRQ)  */
149         COPS_MSG_SSQ,         /* Synchronize State Req (SSQ) */
150         COPS_MSG_OPN,         /* Client-Open (OPN)           */
151         COPS_MSG_CAT,         /* Client-Accept (CAT)         */
152         COPS_MSG_CC,          /* Client-Close (CC)           */
153         COPS_MSG_KA,          /* Keep-Alive (KA)             */
154         COPS_MSG_SSC,         /* Synchronize Complete (SSC)  */
155
156         COPS_LAST_OP_CODE     /* For error checking          */
157 };
158
159 static const value_string cops_op_code_vals[] = {
160         { COPS_MSG_REQ,          "Request (REQ)" },
161         { COPS_MSG_DEC,          "Decision (DEC)" },
162         { COPS_MSG_RPT,          "Report State (RPT)" },
163         { COPS_MSG_DRQ,          "Delete Request State (DRQ)" },
164         { COPS_MSG_SSQ,          "Synchronize State Req (SSQ)" },
165         { COPS_MSG_OPN,          "Client-Open (OPN)" },
166         { COPS_MSG_CAT,          "Client-Accept (CAT)" },
167         { COPS_MSG_CC,           "Client-Close (CC)" },
168         { COPS_MSG_KA,           "Keep-Alive (KA)" },
169         { COPS_MSG_SSC,          "Synchronize Complete (SSC)" },
170         { 0, NULL },
171 };
172
173
174 /* The different objects in COPS messages */
175 enum cops_c_num {
176         COPS_NO_OBJECT,        /* Not a COPS Object type               */
177
178         COPS_OBJ_HANDLE,       /* Handle Object (Handle)               */
179         COPS_OBJ_CONTEXT,      /* Context Object (Context)             */
180         COPS_OBJ_IN_INT,       /* In-Interface Object (IN-Int)         */
181         COPS_OBJ_OUT_INT,      /* Out-Interface Object (OUT-Int)       */
182         COPS_OBJ_REASON,       /* Reason Object (Reason)               */
183         COPS_OBJ_DECISION,     /* Decision Object (Decision)           */
184         COPS_OBJ_LPDPDECISION, /* LPDP Decision Object (LPDPDecision)  */
185         COPS_OBJ_ERROR,        /* Error Object (Error)                 */
186         COPS_OBJ_CLIENTSI,     /* Client Specific Information Object (ClientSI) */
187         COPS_OBJ_KATIMER,      /* Keep-Alive Timer Object (KATimer)    */
188         COPS_OBJ_PEPID,        /* PEP Identification Object (PEPID)    */
189         COPS_OBJ_REPORT_TYPE,  /* Report-Type Object (Report-Type)     */
190         COPS_OBJ_PDPREDIRADDR, /* PDP Redirect Address Object (PDPRedirAddr) */
191         COPS_OBJ_LASTPDPADDR,  /* Last PDP Address (LastPDPaddr)       */
192         COPS_OBJ_ACCTTIMER,    /* Accounting Timer Object (AcctTimer)  */
193         COPS_OBJ_INTEGRITY,    /* Message Integrity Object (Integrity) */
194         COPS_LAST_C_NUM        /* For error checking                   */
195 };
196
197 static const value_string cops_c_num_vals[] = {
198         { COPS_OBJ_HANDLE,       "Handle Object (Handle)" },
199         { COPS_OBJ_CONTEXT,      "Context Object (Context)" },
200         { COPS_OBJ_IN_INT,       "In-Interface Object (IN-Int)" },
201         { COPS_OBJ_OUT_INT,      "Out-Interface Object (OUT-Int)" },
202         { COPS_OBJ_REASON,       "Reason Object (Reason)" },
203         { COPS_OBJ_DECISION,     "Decision Object (Decision)" },
204         { COPS_OBJ_LPDPDECISION, "LPDP Decision Object (LPDPDecision)" },
205         { COPS_OBJ_ERROR,        "Error Object (Error)" },
206         { COPS_OBJ_CLIENTSI,     "Client Specific Information Object (ClientSI)" },
207         { COPS_OBJ_KATIMER,      "Keep-Alive Timer Object (KATimer)" },
208         { COPS_OBJ_PEPID,        "PEP Identification Object (PEPID)" },
209         { COPS_OBJ_REPORT_TYPE,  "Report-Type Object (Report-Type)" },
210         { COPS_OBJ_PDPREDIRADDR, "PDP Redirect Address Object (PDPRedirAddr)" },
211         { COPS_OBJ_LASTPDPADDR,  "Last PDP Address (LastPDPaddr)" },
212         { COPS_OBJ_ACCTTIMER,    "Accounting Timer Object (AcctTimer)" },
213         { COPS_OBJ_INTEGRITY,    "Message Integrity Object (Integrity)" },
214         { 0, NULL },
215 };
216
217
218 /* The different objects in COPS-PR messages */
219 enum cops_s_num {
220         COPS_NO_PR_OBJECT,     /* Not a COPS-PR Object type               */
221         COPS_OBJ_PRID,         /* Provisioning Instance Identifier (PRID) */
222         COPS_OBJ_PPRID,        /* Prefix Provisioning Instance Identifier (PPRID) */
223         COPS_OBJ_EPD,          /* Encoded Provisioning Instance Data (EPD) */
224         COPS_OBJ_GPERR,        /* Global Provisioning Error Object (GPERR) */
225         COPS_OBJ_CPERR,        /* PRC Class Provisioning Error Object (CPERR) */
226         COPS_OBJ_ERRPRID,      /* Error Provisioning Instance Identifier (ErrorPRID)*/
227
228         COPS_LAST_S_NUM        /* For error checking                   */
229 };
230
231
232 static const value_string cops_s_num_vals[] = {
233         { COPS_OBJ_PRID,         "Provisioning Instance Identifier (PRID)" },
234         { COPS_OBJ_PPRID,        "Prefix Provisioning Instance Identifier (PPRID)" },
235         { COPS_OBJ_EPD,          "Encoded Provisioning Instance Data (EPD)" },
236         { COPS_OBJ_GPERR,        "Global Provisioning Error Object (GPERR)" },
237         { COPS_OBJ_CPERR,        "PRC Class Provisioning Error Object (CPERR)" },
238         { COPS_OBJ_ERRPRID,      "Error Provisioning Instance Identifier (ErrorPRID)" },
239         { 0, NULL },
240
241 };
242
243 /* R-Type is carried within the Context Object */
244 static const value_string cops_r_type_vals[] = {
245         { 0x01, "Incoming-Message/Admission Control request" },
246         { 0x02, "Resource-Allocation request" },
247         { 0x04, "Outgoing-Message request" },
248         { 0x08, "Configuration request" },
249         { 0, NULL },
250 };
251 /* S-Type is carried within the ClientSI Object for COPS-PR*/
252 static const value_string cops_s_type_vals[] = {
253         { 0x01, "BER" },
254         { 0, NULL },
255 };
256
257 /* Reason-Code is carried within the Reason object */
258 static const value_string cops_reason_vals[] = {
259         { 1,  "Unspecified" },
260         { 2,  "Management" },
261         { 3,  "Preempted (Another request state takes precedence)" },
262         { 4,  "Tear (Used to communicate a signaled state removal)" },
263         { 5,  "Timeout (Local state has timed-out)" },
264         { 6,  "Route Change (Change invalidates request state)" },
265         { 7,  "Insufficient Resources (No local resource available)" },
266         { 8,  "PDP's Directive (PDP decision caused the delete)" },
267         { 9,  "Unsupported decision (PDP decision not supported)" },
268         { 10, "Synchronize Handle Unknown" },
269         { 11, "Transient Handle (stateless event)" },
270         { 12, "Malformed Decision (could not recover)" },
271         { 13, "Unknown COPS Object from PDP" },
272         { 0, NULL },
273 };
274
275 /* Command-Code is carried within the Decision object if C-Type is 1 */
276 static const value_string cops_dec_cmd_code_vals[] = {
277         { 0, "NULL Decision (No configuration data available)" },
278         { 1, "Install (Admit request/Install configuration)" },
279         { 2, "Remove (Remove request/Remove configuration)" },
280         { 0, NULL },
281 };
282
283 /* Decision flags are also carried with the Decision object if C-Type is 1 */
284 static const value_string cops_dec_cmd_flag_vals[] = {
285         { 0x00, "<None set>" },
286         { 0x01, "Trigger Error (Trigger error message if set)" },
287         { 0, NULL },
288 };
289
290 /* Error-Code from Error object */
291 static const value_string cops_error_vals[] = {
292         {1,  "Bad handle" },
293         {2,  "Invalid handle reference" },
294         {3,  "Bad message format (Malformed Message)" },
295         {4,  "Unable to process (server gives up on query)" },
296         {5,  "Mandatory client-specific info missing" },
297         {6,  "Unsupported client" },
298         {7,  "Mandatory COPS object missing" },
299         {8,  "Client Failure" },
300         {9,  "Communication Failure" },
301         {10, "Unspecified" },
302         {11, "Shutting down" },
303         {12, "Redirect to Preferred Server" },
304         {13, "Unknown COPS Object" },
305         {14, "Authentication Failure" },
306         {15, "Authentication Required" },
307         {0,  NULL },
308 };
309 /* Error-Code from GPERR object */
310 static const value_string cops_gperror_vals[] = {
311         {1,  "AvailMemLow" },
312         {2,  "AvailMemExhausted" },
313         {3,  "unknownASN.1Tag" },
314         {4,  "maxMsgSizeExceeded" },
315         {5,  "unknownError" },
316         {6,  "maxRequestStatesOpen" },
317         {7,  "invalidASN.1Length" },
318         {8,  "invalidObjectPad" },
319         {9,  "unknownPIBData" },
320         {10, "unknownCOPSPRObject" },
321         {11, "malformedDecision" },
322         {0,  NULL },
323 };
324
325 /* Error-Code from CPERR object */
326 static const value_string cops_cperror_vals[] = {
327         {1,  "priSpaceExhausted" },
328         {2,  "priInstanceInvalid" },
329         {3,  "attrValueInvalid" },
330         {4,  "attrValueSupLimited" },
331         {5,  "attrEnumSupLimited" },
332         {6,  "attrMaxLengthExceeded" },
333         {7,  "attrReferenceUnknown" },
334         {8,  "priNotifyOnly" },
335         {9,  "unknownPrc" },
336         {10, "tooFewAttrs" },
337         {11, "invalidAttrType" },
338         {12, "deletedInRef" },
339         {13, "priSpecificError" },
340         {0,  NULL },
341 };
342
343
344 /* Report-Type from Report-Type object */
345 static const value_string cops_report_type_vals[] = {
346         {1, " Success   : Decision was successful at the PEP" },
347         {2, " Failure   : Decision could not be completed by PEP" },
348         {3, " Accounting: Accounting update for an installed state" },
349         {0, NULL },
350 };
351
352 /* Initialize the protocol and registered fields */
353 static gint proto_cops = -1;
354 static gint hf_cops_ver_flags = -1;
355 static gint hf_cops_version = -1;
356 static gint hf_cops_flags = -1;
357
358 static gint hf_cops_op_code = -1;
359 static gint hf_cops_client_type = -1;
360 static gint hf_cops_msg_len = -1;
361
362 static gint hf_cops_obj_len = -1;
363 static gint hf_cops_obj_c_num = -1;
364 static gint hf_cops_obj_c_type = -1;
365
366 static gint hf_cops_obj_s_num = -1;
367 static gint hf_cops_obj_s_type = -1;
368
369 static gint hf_cops_r_type_flags = -1;
370 static gint hf_cops_m_type_flags = -1;
371
372 static gint hf_cops_in_int_ipv4 = -1;
373 static gint hf_cops_in_int_ipv6 = -1;
374 static gint hf_cops_out_int_ipv4 = -1;
375 static gint hf_cops_out_int_ipv6 = -1;
376 static gint hf_cops_int_ifindex = -1;
377
378 static gint hf_cops_reason = -1;
379 static gint hf_cops_reason_sub = -1;
380
381 static gint hf_cops_dec_cmd_code = -1;
382 static gint hf_cops_dec_flags = -1;
383
384 static gint hf_cops_error = -1;
385 static gint hf_cops_error_sub = -1;
386
387 static gint hf_cops_gperror = -1;
388 static gint hf_cops_gperror_sub = -1;
389
390 static gint hf_cops_cperror = -1;
391 static gint hf_cops_cperror_sub = -1;
392
393 static gint hf_cops_katimer = -1;
394
395 static gint hf_cops_pepid = -1;
396
397 static gint hf_cops_report_type = -1;
398
399 static gint hf_cops_pdprediraddr_ipv4 = -1;
400 static gint hf_cops_pdprediraddr_ipv6 = -1;
401 static gint hf_cops_lastpdpaddr_ipv4 = -1;
402 static gint hf_cops_lastpdpaddr_ipv6 = -1;
403 static gint hf_cops_pdp_tcp_port = -1;
404
405 static gint hf_cops_accttimer = -1;
406
407 static gint hf_cops_key_id = -1;
408 static gint hf_cops_seq_num = -1;
409
410 /* Initialize the subtree pointers */
411 static gint ett_cops = -1;
412 static gint ett_cops_ver_flags = -1;
413 static gint ett_cops_obj = -1;
414 static gint ett_cops_pr_obj = -1;
415 static gint ett_cops_obj_data = -1;
416 static gint ett_cops_r_type_flags = -1;
417 static gint ett_cops_itf = -1;
418 static gint ett_cops_reason = -1;
419 static gint ett_cops_decision = -1;
420 static gint ett_cops_error = -1;
421 static gint ett_cops_clientsi = -1;
422 static gint ett_cops_asn1 = -1;
423 static gint ett_cops_gperror = -1;
424 static gint ett_cops_cperror = -1;
425 static gint ett_cops_pdp = -1;
426
427 void proto_reg_handoff_cops(void);
428
429 static guint get_cops_pdu_len(tvbuff_t *tvb, int offset);
430 static void dissect_cops_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
431
432 static int dissect_cops_object(tvbuff_t *tvb, guint32 offset, proto_tree *tree);
433 static void dissect_cops_object_data(tvbuff_t *tvb, guint32 offset, proto_tree *tree,
434                                      guint8 c_num, guint8 c_type, guint16 len);
435
436 static void dissect_cops_pr_objects(tvbuff_t *tvb, guint32 offset, proto_tree *tree, guint16 pr_len);
437 static int dissect_cops_pr_object_data(tvbuff_t *tvb, guint32 offset, proto_tree *tree,
438                                        guint8 s_num, guint8 s_type, guint16 len);
439
440 /* Code to actually dissect the packets */
441 static void
442 dissect_cops(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
443 {
444         tcp_dissect_pdus(tvb, pinfo, tree, cops_desegment, 8,
445             get_cops_pdu_len, dissect_cops_pdu);
446 }
447
448 static guint
449 get_cops_pdu_len(tvbuff_t *tvb, int offset)
450 {
451         /*
452          * Get the length of the COPS message.
453          */
454         return tvb_get_ntohl(tvb, offset + 4);
455 }
456
457 static void
458 dissect_cops_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
459 {
460         guint8 op_code;
461         int object_len;
462
463         if (check_col(pinfo->cinfo, COL_PROTOCOL))
464                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "COPS");
465         if (check_col(pinfo->cinfo, COL_INFO))
466                 col_clear(pinfo->cinfo, COL_INFO);
467
468         op_code = tvb_get_guint8(tvb, 1);
469         if (check_col(pinfo->cinfo, COL_INFO))
470                 col_add_fstr(pinfo->cinfo, COL_INFO, "COPS %s",
471                              val_to_str(op_code, cops_op_code_vals, "Unknown Op Code"));
472
473         if (tree) {
474                 proto_item *ti, *tv;
475                 proto_tree *cops_tree, *ver_flags_tree;
476                 guint32 msg_len;
477                 guint32 offset = 0;
478                 guint8 ver_flags;
479                 gint garbage;
480
481                 ti = proto_tree_add_item(tree, proto_cops, tvb, offset, -1, FALSE);
482                 cops_tree = proto_item_add_subtree(ti, ett_cops);
483
484                 /* Version and flags share the same byte, put them in a subtree */
485                 ver_flags = tvb_get_guint8(tvb, offset);
486                 tv = proto_tree_add_uint_format(cops_tree, hf_cops_ver_flags, tvb, offset, 1,
487                                                   ver_flags, "Version: %u, Flags: %s",
488                                                   hi_nibble(ver_flags),
489                                                   val_to_str(lo_nibble(ver_flags), cops_flags_vals, "Unknown"));
490                 ver_flags_tree = proto_item_add_subtree(tv, ett_cops_ver_flags);
491                 proto_tree_add_uint(ver_flags_tree, hf_cops_version, tvb, offset, 1, ver_flags);
492                 proto_tree_add_uint(ver_flags_tree, hf_cops_flags, tvb, offset, 1, ver_flags);
493                 offset++;
494
495                 proto_tree_add_item(cops_tree, hf_cops_op_code, tvb, offset, 1, FALSE);
496                 offset ++;
497                 proto_tree_add_item(cops_tree, hf_cops_client_type, tvb, offset, 2, FALSE);
498                 offset += 2;
499
500                 msg_len = tvb_get_ntohl(tvb, offset);
501                 proto_tree_add_uint(cops_tree, hf_cops_msg_len, tvb, offset, 4, msg_len);
502                 offset += 4;
503
504                 while (tvb_reported_length_remaining(tvb, offset) >= COPS_OBJECT_HDR_SIZE) {
505                         object_len = dissect_cops_object(tvb, offset, cops_tree);
506                         if (object_len < 0)
507                                 return;
508                         offset += object_len;
509                 }
510
511                 garbage = tvb_length_remaining(tvb, offset);
512                 if (garbage > 0)
513                         proto_tree_add_text(cops_tree, tvb, offset, garbage,
514                                             "Trailing garbage: %d byte%s", garbage,
515                                             plurality(garbage, "", "s"));
516         }
517 }
518
519 static char *cops_c_type_to_str(guint8 c_num, guint8 c_type)
520 {
521         switch (c_num) {
522         case COPS_OBJ_HANDLE:
523                 if (c_type == 1)
524                         return "Client Handle";
525                 break;
526         case COPS_OBJ_IN_INT:
527         case COPS_OBJ_OUT_INT:
528                 if (c_type == 1)
529                         return "IPv4 Address + Interface";
530                 else if (c_type == 2)
531                         return "IPv6 Address + Interface";
532                 break;
533         case COPS_OBJ_DECISION:
534         case COPS_OBJ_LPDPDECISION:
535                 if (c_type == 1)
536                         return "Decision Flags (Mandatory)";
537                 else if (c_type == 2)
538                         return "Stateless Data";
539                 else if (c_type == 3)
540                         return "Replacement Data";
541                 else if (c_type == 4)
542                         return "Client Specific Decision Data";
543                 else if (c_type == 5)
544                         return "Named Decision Data";
545                 break;
546         case COPS_OBJ_CLIENTSI:
547                 if (c_type == 1)
548                         return "Signaled ClientSI";
549                 else if (c_type == 2)
550                         return "Named ClientSI";
551                 break;
552         case COPS_OBJ_KATIMER:
553                 if (c_type == 1)
554                         return "Keep-alive timer value";
555                 break;
556         case COPS_OBJ_PDPREDIRADDR:
557         case COPS_OBJ_LASTPDPADDR:
558                 if (c_type == 1)
559                         return "IPv4 Address + TCP Port";
560                 else if (c_type == 2)
561                         return "IPv6 Address + TCP Port";
562                 break;
563         case COPS_OBJ_ACCTTIMER:
564                 if (c_type == 1)
565                         return "Accounting timer value";
566                 break;
567         case COPS_OBJ_INTEGRITY:
568                 if (c_type == 1)
569                         return "HMAC digest";
570                 break;
571         }
572
573         return "";
574 }
575
576 static int dissect_cops_object(tvbuff_t *tvb, guint32 offset, proto_tree *tree)
577 {
578         int object_len, contents_len;
579         guint8 c_num, c_type;
580         proto_item *ti;
581         proto_tree *obj_tree;
582         char *type_str;
583
584         object_len = tvb_get_ntohs(tvb, offset);
585         if (object_len < COPS_OBJECT_HDR_SIZE) {
586                 /* Bogus! */
587                 proto_tree_add_text(tree, tvb, offset, 2,
588                                     "Bad COPS object length: %u, should be at least %u\n",
589                                     object_len, COPS_OBJECT_HDR_SIZE);
590                 return -1;
591         }
592         c_num = tvb_get_guint8(tvb, offset + 2);
593         c_type = tvb_get_guint8(tvb, offset + 3);
594
595         ti = proto_tree_add_uint_format(tree, hf_cops_obj_c_num, tvb, offset, object_len, c_num,
596                                         "%s: %s", val_to_str(c_num, cops_c_num_vals, "Unknown"),
597                                         cops_c_type_to_str(c_num, c_type));
598         obj_tree = proto_item_add_subtree(ti, ett_cops_obj);
599
600         proto_tree_add_uint(obj_tree, hf_cops_obj_len, tvb, offset, 2, object_len);
601         offset += 2;
602
603         proto_tree_add_uint(obj_tree, hf_cops_obj_c_num, tvb, offset, 1, c_num);
604         offset++;
605
606         type_str = cops_c_type_to_str(c_num, c_type);
607         proto_tree_add_text(obj_tree, tvb, offset, 1, "C-Type: %s%s%u%s",
608                             type_str,
609                             strlen(type_str) ? " (" : "",
610                             c_type,
611                             strlen(type_str) ? ")" : "");
612         offset++;
613
614         contents_len = object_len - COPS_OBJECT_HDR_SIZE;
615         dissect_cops_object_data(tvb, offset, obj_tree, c_num, c_type, contents_len);
616
617         /* Pad to 32bit boundary */
618         if (object_len % sizeof (guint32))
619                 object_len += (sizeof (guint32) - object_len % sizeof (guint32));
620
621         return object_len;
622 }
623
624 static void dissect_cops_pr_objects(tvbuff_t *tvb, guint32 offset, proto_tree *tree, guint16 pr_len)
625 {
626         int object_len, contents_len;
627         guint8 s_num, s_type;
628         char *type_str;
629         int ret;
630         proto_tree *cops_pr_tree, *obj_tree;
631         proto_item *ti;
632
633         cops_pr_tree = proto_item_add_subtree(tree, ett_cops_pr_obj);
634
635         while (pr_len >= COPS_OBJECT_HDR_SIZE) {
636                 object_len = tvb_get_ntohs(tvb, offset);
637                 if (object_len < COPS_OBJECT_HDR_SIZE) {
638                         /* Bogus! */
639                         proto_tree_add_text(tree, tvb, offset, 2,
640                                             "Bad COPS PR object length: %u, should be at least %u\n",
641                                             object_len, COPS_OBJECT_HDR_SIZE);
642                         return;
643                 }
644                 s_num = tvb_get_guint8(tvb, offset + 2);
645
646                 ti = proto_tree_add_uint_format(cops_pr_tree, hf_cops_obj_s_num, tvb, offset, object_len, s_num,
647                                         "%s", val_to_str(s_num, cops_s_num_vals, "Unknown"));
648                 obj_tree = proto_item_add_subtree(cops_pr_tree, ett_cops_pr_obj);
649
650                 proto_tree_add_uint(obj_tree, hf_cops_obj_len, tvb, offset, 2, object_len);
651                 offset += 2;
652                 pr_len -= 2;
653
654                 proto_tree_add_uint(obj_tree, hf_cops_obj_s_num, tvb, offset, 1, s_num);
655                 offset++;
656                 pr_len--;
657
658                 s_type = tvb_get_guint8(tvb, offset);
659                 type_str = val_to_str(s_type, cops_s_type_vals, "Unknown");
660                 proto_tree_add_text(obj_tree, tvb, offset, 1, "S-Type: %s%s%u%s",
661                             type_str,
662                             strlen(type_str) ? " (" : "",
663                             s_type,
664                             strlen(type_str) ? ")" : "");
665                 offset++;
666                 pr_len--;
667
668                 contents_len = object_len - COPS_OBJECT_HDR_SIZE;
669                 ret = dissect_cops_pr_object_data(tvb, offset, obj_tree, s_num, s_type, contents_len);
670                 if (ret < 0)
671                         break;
672
673                 /*Pad to 32bit boundary */
674                 if (object_len % sizeof (guint32))
675                         object_len += (sizeof (guint32) - object_len % sizeof (guint32));
676
677                 pr_len -= object_len - COPS_OBJECT_HDR_SIZE;
678                 offset += object_len - COPS_OBJECT_HDR_SIZE;
679         }
680 }
681
682 static void dissect_cops_object_data(tvbuff_t *tvb, guint32 offset, proto_tree *tree,
683                                      guint8 c_num, guint8 c_type, guint16 len)
684 {
685         proto_item *ti;
686         proto_tree *r_type_tree, *itf_tree, *reason_tree, *dec_tree, *error_tree, *clientsi_tree, *pdp_tree;
687         guint16 r_type, m_type, reason, reason_sub, cmd_code, cmd_flags, error, error_sub, tcp_port;
688         guint32 ipv4addr, ifindex;
689         struct e_in6_addr ipv6addr;
690
691         switch (c_num) {
692         case COPS_OBJ_CONTEXT:
693                 r_type = tvb_get_ntohs(tvb, offset);
694                 m_type = tvb_get_ntohs(tvb, offset + 2);
695                 ti = proto_tree_add_text(tree, tvb, offset, 4, "Contents: R-Type: %s, M-Type: %u",
696                                          val_to_str(r_type, cops_r_type_vals, "Unknown"),
697                                          m_type);
698
699                 r_type_tree = proto_item_add_subtree(ti, ett_cops_r_type_flags);
700                 proto_tree_add_uint(r_type_tree, hf_cops_r_type_flags, tvb, offset, 2, r_type);
701                 offset += 2;
702                 proto_tree_add_uint(r_type_tree, hf_cops_m_type_flags, tvb, offset, 2, m_type);
703
704                 break;
705         case COPS_OBJ_IN_INT:
706         case COPS_OBJ_OUT_INT:
707                 if (c_type == 1) {          /* IPv4 */
708                         tvb_memcpy(tvb, (guint8 *)&ipv4addr, offset, 4);
709                         ifindex = tvb_get_ntohl(tvb, offset + 4);
710                         ti = proto_tree_add_text(tree, tvb, offset, 8, "Contents: IPv4 address %s, ifIndex: %u",
711                                                  ip_to_str((guint8 *)&ipv4addr), ifindex);
712                         itf_tree = proto_item_add_subtree(ti, ett_cops_itf);
713                         proto_tree_add_ipv4(itf_tree,
714                                             (c_num == COPS_OBJ_IN_INT) ? hf_cops_in_int_ipv4 : hf_cops_out_int_ipv4,
715                                             tvb, offset, 4, ipv4addr);
716                         offset += 4;
717                 } else if (c_type == 2) {   /* IPv6 */
718                         tvb_memcpy(tvb, (guint8 *)&ipv6addr, offset, sizeof ipv6addr);
719                         ifindex = tvb_get_ntohl(tvb, offset + sizeof ipv6addr);
720                         ti = proto_tree_add_text(tree, tvb, offset, 20, "Contents: IPv6 address %s, ifIndex: %u",
721                                                  ip6_to_str(&ipv6addr), ifindex);
722                         itf_tree = proto_item_add_subtree(ti, ett_cops_itf);
723                         proto_tree_add_ipv6(itf_tree,
724                                             (c_num == COPS_OBJ_IN_INT) ? hf_cops_in_int_ipv6 : hf_cops_out_int_ipv6,
725                                             tvb, offset, 16, (guint8 *)&ipv6addr);
726                         offset += 16;
727                 } else {
728                         break;
729                 }
730                 proto_tree_add_uint(itf_tree, hf_cops_int_ifindex, tvb, offset, 4, ifindex);
731
732                 break;
733         case COPS_OBJ_REASON:
734                 reason = tvb_get_ntohs(tvb, offset);
735                 reason_sub = tvb_get_ntohs(tvb, offset + 2);
736                 ti = proto_tree_add_text(tree, tvb, offset, 4, "Contents: Reason-Code: %s, Reason Sub-code: 0x%04x",
737                                          val_to_str(reason, cops_reason_vals, "<Unknown value>"), reason_sub);
738                 reason_tree = proto_item_add_subtree(ti, ett_cops_reason);
739                 proto_tree_add_uint(reason_tree, hf_cops_reason, tvb, offset, 2, reason);
740                 offset += 2;
741                 if (reason == 13) {
742                         proto_tree_add_text(reason_tree, tvb, offset, 2, "Reason Sub-code: "
743                                             "Unknown object's C-Num %u, C-Type %u",
744                                             tvb_get_guint8(tvb, offset), tvb_get_guint8(tvb, offset + 1));
745                 } else
746                         proto_tree_add_uint(reason_tree, hf_cops_reason_sub, tvb, offset, 2, reason_sub);
747
748                 break;
749         case COPS_OBJ_DECISION:
750         case COPS_OBJ_LPDPDECISION:
751                 if (c_type == 1) {
752                         cmd_code = tvb_get_ntohs(tvb, offset);
753                         cmd_flags = tvb_get_ntohs(tvb, offset + 2);
754                         ti = proto_tree_add_text(tree, tvb, offset, 4, "Contents: Command-Code: %s, Flags: %s",
755                                          val_to_str(cmd_code, cops_dec_cmd_code_vals, "<Unknown value>"),
756                                          val_to_str(cmd_flags, cops_dec_cmd_flag_vals, "<Unknown flag>"));
757                         dec_tree = proto_item_add_subtree(ti, ett_cops_decision);
758                         proto_tree_add_uint(dec_tree, hf_cops_dec_cmd_code, tvb, offset, 2, cmd_code);
759                         offset += 2;
760                         proto_tree_add_uint(dec_tree, hf_cops_dec_flags, tvb, offset, 2, cmd_flags);
761                 } else if (c_type == 5) { /*COPS-PR Data*/
762                         ti = proto_tree_add_text(tree, tvb, offset, 4, "Contents: %u bytes", len);
763                         dec_tree = proto_item_add_subtree(ti, ett_cops_decision);
764                         dissect_cops_pr_objects(tvb, offset, dec_tree, len);
765                 }
766
767                 break;
768         case COPS_OBJ_ERROR:
769                 if (c_type != 1)
770                         break;
771
772                 error = tvb_get_ntohs(tvb, offset);
773                 error_sub = tvb_get_ntohs(tvb, offset + 2);
774                 ti = proto_tree_add_text(tree, tvb, offset, 4, "Contents: Error-Code: %s, Error Sub-code: 0x%04x",
775                                          val_to_str(error, cops_error_vals, "<Unknown value>"), error_sub);
776                 error_tree = proto_item_add_subtree(ti, ett_cops_error);
777                 proto_tree_add_uint(error_tree, hf_cops_error, tvb, offset, 2, error);
778                 offset += 2;
779                 if (error == 13) {
780                         proto_tree_add_text(error_tree, tvb, offset, 2, "Error Sub-code: "
781                                             "Unknown object's C-Num %u, C-Type %u",
782                                             tvb_get_guint8(tvb, offset), tvb_get_guint8(tvb, offset + 1));
783                 } else
784                         proto_tree_add_uint(error_tree, hf_cops_error_sub, tvb, offset, 2, error_sub);
785
786                 break;
787         case COPS_OBJ_CLIENTSI:
788
789                 if (c_type != 2) /*Not COPS-PR data*/
790                       break;
791
792                 ti = proto_tree_add_text(tree, tvb, offset, 4, "Contents: %u bytes", len);
793                 clientsi_tree = proto_item_add_subtree(ti, ett_cops_clientsi);
794
795                 dissect_cops_pr_objects(tvb, offset, clientsi_tree, len);
796
797                 break;
798         case COPS_OBJ_KATIMER:
799                 if (c_type != 1)
800                         break;
801
802                 proto_tree_add_item(tree, hf_cops_katimer, tvb, offset + 2, 2, FALSE);
803                 if (tvb_get_ntohs(tvb, offset + 2) == 0)
804                         proto_tree_add_text(tree, tvb, offset, 0, "Value of zero implies infinity.");
805
806                 break;
807         case COPS_OBJ_PEPID:
808                 if (c_type != 1)
809                         break;
810
811                 if (tvb_strnlen(tvb, offset, len) == -1)
812                         proto_tree_add_text(tree, tvb, offset, len, "<PEP Id is not a NUL terminated ASCII string>");
813                 else
814                         proto_tree_add_item(tree, hf_cops_pepid, tvb, offset,
815                                             tvb_strnlen(tvb, offset, len) + 1, FALSE);
816
817                 break;
818         case COPS_OBJ_REPORT_TYPE:
819                 if (c_type != 1)
820                         break;
821
822                 proto_tree_add_item(tree, hf_cops_report_type, tvb, offset, 2, FALSE);
823
824                 break;
825         case COPS_OBJ_PDPREDIRADDR:
826         case COPS_OBJ_LASTPDPADDR:
827                 if (c_type == 1) {          /* IPv4 */
828                         tvb_memcpy(tvb, (guint8 *)&ipv4addr, offset, 4);
829                         tcp_port = tvb_get_ntohs(tvb, offset + 4 + 2);
830                         ti = proto_tree_add_text(tree, tvb, offset, 8, "Contents: IPv4 address %s, TCP Port Number: %u",
831                                                  ip_to_str((guint8 *)&ipv4addr), tcp_port);
832                         pdp_tree = proto_item_add_subtree(ti, ett_cops_pdp);
833                         proto_tree_add_ipv4(pdp_tree,
834                                             (c_num == COPS_OBJ_PDPREDIRADDR) ? hf_cops_pdprediraddr_ipv4 : hf_cops_lastpdpaddr_ipv4,
835                                             tvb, offset, 4, ipv4addr);
836                         offset += 4;
837                 } else if (c_type == 2) {   /* IPv6 */
838                         tvb_memcpy(tvb, (guint8 *)&ipv6addr, offset, sizeof ipv6addr);
839                         tcp_port = tvb_get_ntohs(tvb, offset + sizeof ipv6addr + 2);
840                         ti = proto_tree_add_text(tree, tvb, offset, 20, "Contents: IPv6 address %s, TCP Port Number: %u",
841                                                  ip6_to_str(&ipv6addr), tcp_port);
842                         pdp_tree = proto_item_add_subtree(ti, ett_cops_pdp);
843                         proto_tree_add_ipv6(pdp_tree,
844                                             (c_num == COPS_OBJ_PDPREDIRADDR) ? hf_cops_pdprediraddr_ipv6 : hf_cops_lastpdpaddr_ipv6,
845                                             tvb, offset, 16, (guint8 *)&ipv6addr);
846                         offset += 16;
847                 } else {
848                         break;
849                 }
850                 offset += 2;
851                 proto_tree_add_uint(pdp_tree, hf_cops_pdp_tcp_port, tvb, offset, 2, tcp_port);
852
853                 break;
854         case COPS_OBJ_ACCTTIMER:
855                 if (c_type != 1)
856                         break;
857
858                 proto_tree_add_item(tree, hf_cops_accttimer, tvb, offset + 2, 2, FALSE);
859                 if (tvb_get_ntohs(tvb, offset + 2) == 0)
860                         proto_tree_add_text(tree, tvb, offset, 0, "Value of zero means "
861                                             "there SHOULD be no unsolicited accounting updates.");
862
863                 break;
864         case COPS_OBJ_INTEGRITY:
865                 if (c_type != 1)
866                         break;      /* Not HMAC digest */
867
868                 proto_tree_add_item(tree, hf_cops_key_id, tvb, offset, 4, FALSE);
869                 proto_tree_add_item(tree, hf_cops_seq_num, tvb, offset + 4, 4, FALSE);
870                 proto_tree_add_text(tree, tvb, offset + 8 , len - 8, "Contents: Keyed Message Digest");
871
872                 break;
873         default:
874                 proto_tree_add_text(tree, tvb, offset, len, "Contents: %u bytes", len);
875
876                 break;
877         }
878 }
879
880
881 /*convert hex to binary string (1010....)*/
882 static gchar* xtobstr(guint8 *hex, guint len) {
883
884   guint i=0,j=0,k=0, bit=0;
885   gchar *binstr=NULL;
886   guint8 mask = 0x80;
887
888   binstr = g_malloc(8*sizeof(gchar)*len);
889
890   for (i=0; i < len; i++) {
891     for ( j=0; j<8; j++ ) {   /* for each bit     */
892       bit = (mask & hex[i]) ? 1 : 0;  /* bit is 1 or 0    */
893       sprintf(&binstr[k++],"%d",bit); /*put it in string */
894       mask >>= 1;    /* shift mask right */
895     }
896     mask = 0x80;
897   }
898   return binstr;
899 }
900
901 /*
902  * XXX - we should perhaps support reading PIBs, as we support reading
903  * MIBs, and use the PIBs we read to understand how to display COPS data
904  * just as we use the MIBs we read to understand how to display SNMP
905  * data.
906  */
907 static int decode_cops_pr_asn1_data(tvbuff_t *tvb, guint32 offset,
908     proto_tree *tree, guint epdlen, gboolean inepd)
909 {
910         ASN1_SCK asn1;
911         int start;
912         gboolean def;
913         guint length;
914
915         guint vb_length;
916         gushort vb_type;
917         gchar *vb_type_name;
918
919         int ret;
920         guint cls, con, tag;
921
922         gint32 vb_integer_value;
923         guint32 vb_uinteger_value;
924
925         guint8 *vb_octet_string;
926
927         subid_t *vb_oid;
928         guint vb_oid_length;
929
930         gchar *vb_display_string;
931
932         unsigned int i=0;
933         gchar *buf;
934         int len;
935
936         while (epdlen > 0) { /*while there is stuff to be decoded*/
937                 asn1_open(&asn1, tvb, offset);
938
939                 /* parse the type of the object */
940
941                 start = asn1.offset;
942
943                 ret = asn1_header_decode (&asn1, &cls, &con, &tag, &def,
944                     &vb_length);
945                 if (ret != ASN1_ERR_NOERROR)
946                         return 0;
947                 if (!def)
948                         return ASN1_ERR_LENGTH_NOT_DEFINITE;
949
950                 /* Convert the class, constructed flag, and tag to a type. */
951                 vb_type_name = cops_tag_cls2syntax(tag, cls, &vb_type);
952                 if (vb_type_name == NULL) {
953                         /*
954                          * Unsupported type.
955                          * Dissect the value as an opaque string of octets.
956                          */
957                         vb_type_name = "unsupported type";
958                         vb_type = COPS_OPAQUE;
959                 }
960
961                 /* parse the value */
962
963                 switch (vb_type) {
964
965                 case COPS_INTEGER:
966                         ret = asn1_int32_value_decode(&asn1, vb_length,
967                             &vb_integer_value);
968                         if (ret != ASN1_ERR_NOERROR)
969                                 return ret;
970                         length = asn1.offset - start;
971                         if (tree) {
972                                 proto_tree_add_text(tree, asn1.tvb, offset, length,
973                                     "Value: %s: %d (%#x)", vb_type_name,
974                                     vb_integer_value, vb_integer_value);
975                         }
976                         break;
977
978                 case COPS_UNSIGNED32:
979                 case COPS_TIMETICKS:
980                         ret = asn1_uint32_value_decode(&asn1, vb_length,
981                             &vb_uinteger_value);
982                         if (ret != ASN1_ERR_NOERROR)
983                                 return ret;
984                         length = asn1.offset - start;
985                         if (tree) {
986                                 proto_tree_add_text(tree, asn1.tvb, offset, length,
987                                     "Value: %s: %u (%#x)", vb_type_name,
988                                     vb_uinteger_value, vb_uinteger_value);
989                         }
990                         break;
991
992                 case COPS_OCTETSTR:
993                 case COPS_IPADDR:
994                 case COPS_OPAQUE:
995                 case COPS_UNSIGNED64:
996                 case COPS_INTEGER64:
997                         ret = asn1_string_value_decode (&asn1, vb_length,
998                             &vb_octet_string);
999                         if (ret != ASN1_ERR_NOERROR)
1000                                 return ret;
1001                         length = asn1.offset - start;
1002                         if (tree) {
1003                                 /* if this EPD belongs to ipFilter or
1004                                    frwkPrcSupport Entries print it correctly) */
1005                                 if ((strncmp(last_decoded_prid,"1.3.6.1.2.2.2.3.2.1",19)==0) || (strncmp(last_decoded_prid,"1.3.6.1.2.2.2.1.1.1",19)==0)) {
1006                                         if(strncmp(last_decoded_prid,"1.3.6.1.2.2.2.3.2.1",19)==0) {
1007                                                 i=0;/* EPD belongs to
1008                                                        IpFilters, print as
1009                                                        bytes (IPv6 not printed
1010                                                        ok - yet)*/
1011                                         } else {
1012                                                 /* EPD belongs to
1013                                                    frwkPrcSupportEntry,
1014                                                    convert hex byte(s) to
1015                                                    binary string*/
1016                                                 vb_display_string =
1017                                                     xtobstr(vb_octet_string,
1018                                                       vb_length);
1019
1020                                                 proto_tree_add_text(tree, asn1.tvb,
1021                                                     offset, length,
1022                                                     "Value: %s: %s", vb_type_name,
1023                                                     vb_display_string);
1024
1025                                                 g_free(vb_octet_string);
1026                                                 g_free(vb_display_string);
1027
1028                                                 break;
1029                                         }
1030                                 } else {
1031                                         /* EPD doesn't belong to ipFilter or
1032                                            frwkPrcSupport Entries;
1033                                            check for unprintable characters*/
1034
1035                                         for (i = 0; i < vb_length; i++) {
1036                                                 if (!(isprint(vb_octet_string[i])
1037                                                     ||isspace(vb_octet_string[i])))
1038                                                         break;
1039                                         }
1040                                 }
1041
1042                                 /*
1043                                  * If some characters are not printable,
1044                                  * display the string as bytes.
1045                                  */
1046                                 if (i < vb_length) {
1047                                         /*
1048                                          * We stopped, due to a non-printable
1049                                          * character, before we got to the end
1050                                          * of the string.
1051                                          */
1052                                         vb_display_string =
1053                                             g_malloc(4*vb_length);
1054                                         buf = &vb_display_string[0];
1055                                         len = sprintf(buf, "%03u",
1056                                             vb_octet_string[0]);
1057                                         buf += len;
1058                                         for (i = 1; i < vb_length; i++) {
1059                                                 len = sprintf(buf, ".%03u",
1060                                                     vb_octet_string[i]);
1061                                                 buf += len;
1062                                         }
1063                                         proto_tree_add_text(tree,
1064                                             asn1.tvb, offset, length,
1065                                             "Value: %s: %s", vb_type_name,
1066                                             vb_display_string);
1067                                         g_free(vb_display_string);
1068                                 } else {
1069                                         proto_tree_add_text(tree,
1070                                             asn1.tvb, offset, length,
1071                                             "Value: %s: %.*s", vb_type_name,
1072                                             (int)vb_length,
1073                                             SAFE_STRING(vb_octet_string));
1074                                 }
1075                         }
1076                         g_free(vb_octet_string);
1077                         break;
1078
1079                 case COPS_NULL:
1080                         ret = asn1_null_decode (&asn1, vb_length);
1081                         if (ret != ASN1_ERR_NOERROR)
1082                                 return ret;
1083                         length = asn1.offset - start;
1084                         if (tree) {
1085                                 proto_tree_add_text(tree, asn1.tvb, offset, length,
1086                                     "Value: %s", vb_type_name);
1087                         }
1088                         break;
1089
1090                 case COPS_OBJECTID:
1091                         ret = asn1_oid_value_decode (&asn1, vb_length, &vb_oid,
1092                             &vb_oid_length);
1093                         if (ret != ASN1_ERR_NOERROR)
1094                                 return ret;
1095                         length = asn1.offset - start;
1096
1097                         if (tree) {
1098                                 vb_display_string = format_oid(vb_oid,
1099                                     vb_oid_length);
1100                                 proto_tree_add_text(tree, asn1.tvb, offset, length,
1101                                     "Value: %s: %s", vb_type_name,
1102                                     vb_display_string);
1103
1104                                 if (inepd) {
1105                                         /* we're decoding EPD */
1106                                         g_free(vb_display_string);
1107                                 } else {
1108                                         /* we're decoding PRID, so let's store
1109                                            the OID of the PRID so that later
1110                                            when we're decoding this PRID's EPD
1111                                            we can finetune the output. */
1112                                         if (last_decoded_prid)
1113                                                 g_free(last_decoded_prid);
1114                                         last_decoded_prid = vb_display_string;
1115                                 }
1116                         }
1117                         g_free(vb_oid);
1118                         break;
1119
1120                 default:
1121                         g_assert_not_reached();
1122                         return ASN1_ERR_WRONG_TYPE;
1123                 }
1124
1125                 asn1_close(&asn1,&offset);
1126
1127                 epdlen -= length;
1128         }
1129         return 0;
1130 }
1131
1132 static int dissect_cops_pr_object_data(tvbuff_t *tvb, guint32 offset, proto_tree *tree,
1133                                     guint8 s_num, guint8 s_type, guint16 len)
1134 {
1135         proto_item *ti;
1136         proto_tree *asn1_object_tree, *gperror_tree, *cperror_tree;
1137         guint16 gperror=0, gperror_sub=0, cperror=0, cperror_sub=0;
1138
1139         switch (s_num){
1140         case COPS_OBJ_PRID:
1141                if (s_type != 1) /* Not Provisioning Instance Identifier (PRID) */
1142                         break;
1143
1144                 ti=proto_tree_add_text(tree, tvb, offset, len, "Contents:");
1145                 asn1_object_tree = proto_item_add_subtree(ti, ett_cops_asn1);
1146
1147                 decode_cops_pr_asn1_data(tvb, offset, asn1_object_tree, len, FALSE);
1148
1149                 break;
1150         case COPS_OBJ_PPRID:
1151                 if (s_type != 1) /* Not Prefix Provisioning Instance Identifier (PPRID) */
1152                         break;
1153
1154                 ti = proto_tree_add_text(tree, tvb, offset, len, "Contents:");
1155                 asn1_object_tree = proto_item_add_subtree(ti, ett_cops_asn1);
1156
1157                 decode_cops_pr_asn1_data(tvb, offset, asn1_object_tree, len, FALSE);
1158
1159                 break;
1160         case COPS_OBJ_EPD:
1161                 if (s_type != 1) /* Not  Encoded Provisioning Instance Data (EPD) */
1162                         break;
1163
1164                 ti = proto_tree_add_text(tree, tvb, offset, len, "Contents:");
1165                 asn1_object_tree = proto_item_add_subtree(ti, ett_cops_asn1);
1166
1167                 decode_cops_pr_asn1_data(tvb, offset, asn1_object_tree, len, TRUE);
1168
1169                 break;
1170         case COPS_OBJ_GPERR:
1171                 if (s_type != 1) /* Not Global Provisioning Error Object (GPERR) */
1172                         break;
1173
1174                 gperror = tvb_get_ntohs(tvb, offset);
1175                 gperror_sub = tvb_get_ntohs(tvb, offset + 2);
1176                 ti = proto_tree_add_text(tree, tvb, offset, 4, "Contents: Error-Code: %s, Error Sub-code: 0x%04x",
1177                                          val_to_str(gperror, cops_gperror_vals, "<Unknown value>"), gperror_sub);
1178                 gperror_tree = proto_item_add_subtree(ti, ett_cops_gperror);
1179                 proto_tree_add_uint(gperror_tree, hf_cops_gperror, tvb, offset, 2, gperror);
1180                 offset += 2;
1181                 if (cperror == 13) {
1182                         proto_tree_add_text(gperror_tree, tvb, offset, 2, "Error Sub-code: "
1183                                             "Unknown object's C-Num %u, C-Type %u",
1184                                             tvb_get_guint8(tvb, offset), tvb_get_guint8(tvb, offset + 1));
1185                 } else
1186                         proto_tree_add_uint(gperror_tree, hf_cops_gperror_sub, tvb, offset, 2, gperror_sub);
1187
1188                 break;
1189         case COPS_OBJ_CPERR:
1190                 if (s_type != 1) /*Not PRC Class Provisioning Error Object (CPERR) */
1191                         break;
1192
1193                 break;
1194
1195                 cperror = tvb_get_ntohs(tvb, offset);
1196                 cperror_sub = tvb_get_ntohs(tvb, offset + 2);
1197                 ti = proto_tree_add_text(tree, tvb, offset, 4, "Contents: Error-Code: %s, Error Sub-code: 0x%04x",
1198                                          val_to_str(cperror, cops_cperror_vals, "<Unknown value>"), cperror_sub);
1199                 cperror_tree = proto_item_add_subtree(ti, ett_cops_cperror);
1200                 proto_tree_add_uint(cperror_tree, hf_cops_cperror, tvb, offset, 2, cperror);
1201                 offset += 2;
1202                 if (cperror == 13) {
1203                         proto_tree_add_text(cperror_tree, tvb, offset, 2, "Error Sub-code: "
1204                                             "Unknown object's S-Num %u, C-Type %u",
1205                                             tvb_get_guint8(tvb, offset), tvb_get_guint8(tvb, offset + 1));
1206                 } else
1207                         proto_tree_add_uint(cperror_tree, hf_cops_cperror_sub, tvb, offset, 2, cperror_sub);
1208
1209                 break;
1210         case COPS_OBJ_ERRPRID:
1211                 if (s_type != 1) /*Not  Error Provisioning Instance Identifier (ErrorPRID)*/
1212                         break;
1213
1214                 ti = proto_tree_add_text(tree, tvb, offset, len, "Contents:");
1215                 asn1_object_tree = proto_item_add_subtree(ti, ett_cops_asn1);
1216
1217                 decode_cops_pr_asn1_data(tvb, offset, asn1_object_tree, len, FALSE);
1218
1219                 break;
1220         default:
1221                 proto_tree_add_text(tree, tvb, offset, len, "Contents: %u bytes", len);
1222                 break;
1223         }
1224
1225         return 0;
1226 }
1227
1228
1229 /* Register the protocol with Ethereal */
1230 void proto_register_cops(void)
1231 {
1232         /* Setup list of header fields */
1233         static hf_register_info hf[] = {
1234                 { &hf_cops_ver_flags,
1235                         { "Version and Flags",           "cops.ver_flags",
1236                         FT_UINT8, BASE_HEX, NULL, 0x0,
1237                         "Version and Flags in COPS Common Header", HFILL }
1238                 },
1239                 { &hf_cops_version,
1240                         { "Version",           "cops.version",
1241                         FT_UINT8, BASE_DEC, NULL, 0xF0,
1242                         "Version in COPS Common Header", HFILL }
1243                 },
1244                 { &hf_cops_flags,
1245                         { "Flags",           "cops.flags",
1246                         FT_UINT8, BASE_HEX, VALS(cops_flags_vals), 0x0F,
1247                         "Flags in COPS Common Header", HFILL }
1248                 },
1249                 { &hf_cops_op_code,
1250                         { "Op Code",           "cops.op_code",
1251                         FT_UINT8, BASE_DEC, VALS(cops_op_code_vals), 0x0,
1252                         "Op Code in COPS Common Header", HFILL }
1253                 },
1254                 { &hf_cops_client_type,
1255                         { "Client Type",           "cops.client_type",
1256                         FT_UINT16, BASE_DEC, NULL, 0x0,
1257                         "Client Type in COPS Common Header", HFILL }
1258                 },
1259                 { &hf_cops_msg_len,
1260                         { "Message Length",           "cops.msg_len",
1261                         FT_UINT32, BASE_DEC, NULL, 0x0,
1262                         "Message Length in COPS Common Header", HFILL }
1263                 },
1264                 { &hf_cops_obj_len,
1265                         { "Object Length",           "cops.obj.len",
1266                         FT_UINT32, BASE_DEC, NULL, 0x0,
1267                         "Object Length in COPS Object Header", HFILL }
1268                 },
1269                 { &hf_cops_obj_c_num,
1270                         { "C-Num",           "cops.c_num",
1271                         FT_UINT8, BASE_DEC, VALS(cops_c_num_vals), 0x0,
1272                         "C-Num in COPS Object Header", HFILL }
1273                 },
1274                 { &hf_cops_obj_c_type,
1275                         { "C-Type",           "cops.c_type",
1276                         FT_UINT8, BASE_DEC, NULL, 0x0,
1277                         "C-Type in COPS Object Header", HFILL }
1278                 },
1279
1280                 { &hf_cops_obj_s_num,
1281                         { "S-Num",           "cops.s_num",
1282                         FT_UINT8, BASE_DEC, VALS(cops_s_num_vals), 0x0,
1283                         "S-Num in COPS-PR Object Header", HFILL }
1284                 },
1285                 { &hf_cops_obj_s_type,
1286                         { "S-Type",           "cops.s_type",
1287                         FT_UINT8, BASE_DEC, NULL, 0x0,
1288                         "S-Type in COPS-PR Object Header", HFILL }
1289                 },
1290
1291                 { &hf_cops_r_type_flags,
1292                         { "R-Type",           "cops.context.r_type",
1293                         FT_UINT16, BASE_HEX, VALS(cops_r_type_vals), 0xFFFF,
1294                         "R-Type in COPS Context Object", HFILL }
1295                 },
1296                 { &hf_cops_m_type_flags,
1297                         { "M-Type",           "cops.context.m_type",
1298                         FT_UINT16, BASE_HEX, NULL, 0xFFFF,
1299                         "M-Type in COPS Context Object", HFILL }
1300                 },
1301                 { &hf_cops_in_int_ipv4,
1302                         { "IPv4 address",           "cops.in-int.ipv4",
1303                         FT_IPv4, 0, NULL, 0xFFFF,
1304                         "IPv4 address in COPS IN-Int object", HFILL }
1305                 },
1306                 { &hf_cops_in_int_ipv6,
1307                         { "IPv6 address",           "cops.in-int.ipv6",
1308                         FT_IPv6, 0, NULL, 0xFFFF,
1309                         "IPv6 address in COPS IN-Int object", HFILL }
1310                 },
1311                 { &hf_cops_out_int_ipv4,
1312                         { "IPv4 address",           "cops.out-int.ipv4",
1313                         FT_IPv4, 0, NULL, 0xFFFF,
1314                         "IPv4 address in COPS OUT-Int object", HFILL }
1315                 },
1316                 { &hf_cops_out_int_ipv6,
1317                         { "IPv6 address",           "cops.out-int.ipv6",
1318                         FT_IPv6, 0, NULL, 0xFFFF,
1319                         "IPv6 address in COPS OUT-Int", HFILL }
1320                 },
1321                 { &hf_cops_int_ifindex,
1322                         { "ifIndex",           "cops.in-out-int.ifindex",
1323                         FT_UINT32, BASE_DEC, NULL, 0x0,
1324                         "If SNMP is supported, corresponds to MIB-II ifIndex", HFILL }
1325                 },
1326                 { &hf_cops_reason,
1327                         { "Reason",           "cops.reason",
1328                         FT_UINT16, BASE_DEC, VALS(cops_reason_vals), 0,
1329                         "Reason in Reason object", HFILL }
1330                 },
1331                 { &hf_cops_reason_sub,
1332                         { "Reason Sub-code",           "cops.reason_sub",
1333                         FT_UINT16, BASE_HEX, NULL, 0,
1334                         "Reason Sub-code in Reason object", HFILL }
1335                 },
1336                 { &hf_cops_dec_cmd_code,
1337                         { "Command-Code",           "cops.decision.cmd",
1338                         FT_UINT16, BASE_DEC, VALS(cops_dec_cmd_code_vals), 0,
1339                         "Command-Code in Decision/LPDP Decision object", HFILL }
1340                 },
1341                 { &hf_cops_dec_flags,
1342                         { "Flags",           "cops.decision.flags",
1343                         FT_UINT16, BASE_HEX, VALS(cops_dec_cmd_flag_vals), 0xffff,
1344                         "Flags in Decision/LPDP Decision object", HFILL }
1345                 },
1346                 { &hf_cops_error,
1347                         { "Error",           "cops.error",
1348                         FT_UINT16, BASE_DEC, VALS(cops_error_vals), 0,
1349                         "Error in Error object", HFILL }
1350                 },
1351                 { &hf_cops_error_sub,
1352                         { "Error Sub-code",           "cops.error_sub",
1353                         FT_UINT16, BASE_HEX, NULL, 0,
1354                         "Error Sub-code in Error object", HFILL }
1355                 },
1356                 { &hf_cops_katimer,
1357                         { "Contents: KA Timer Value",           "cops.katimer.value",
1358                         FT_UINT16, BASE_DEC, NULL, 0,
1359                         "Keep-Alive Timer Value in KATimer object", HFILL }
1360                 },
1361                 { &hf_cops_pepid,
1362                         { "Contents: PEP Id",           "cops.pepid.id",
1363                         FT_STRING, BASE_NONE, NULL, 0,
1364                         "PEP Id in PEPID object", HFILL }
1365                 },
1366                 { &hf_cops_report_type,
1367                         { "Contents: Report-Type",           "cops.report_type",
1368                         FT_UINT16, BASE_DEC, VALS(cops_report_type_vals), 0,
1369                         "Report-Type in Report-Type object", HFILL }
1370                 },
1371                 { &hf_cops_pdprediraddr_ipv4,
1372                         { "IPv4 address",           "cops.pdprediraddr.ipv4",
1373                         FT_IPv4, 0, NULL, 0xFFFF,
1374                         "IPv4 address in COPS PDPRedirAddr object", HFILL }
1375                 },
1376                 { &hf_cops_pdprediraddr_ipv6,
1377                         { "IPv6 address",           "cops.pdprediraddr.ipv6",
1378                         FT_IPv6, 0, NULL, 0xFFFF,
1379                         "IPv6 address in COPS PDPRedirAddr object", HFILL }
1380                 },
1381                 { &hf_cops_lastpdpaddr_ipv4,
1382                         { "IPv4 address",           "cops.lastpdpaddr.ipv4",
1383                         FT_IPv4, 0, NULL, 0xFFFF,
1384                         "IPv4 address in COPS LastPDPAddr object", HFILL }
1385                 },
1386                 { &hf_cops_lastpdpaddr_ipv6,
1387                         { "IPv6 address",           "cops.lastpdpaddr.ipv6",
1388                         FT_IPv6, 0, NULL, 0xFFFF,
1389                         "IPv6 address in COPS LastPDPAddr object", HFILL }
1390                 },
1391                 { &hf_cops_pdp_tcp_port,
1392                         { "TCP Port Number",           "cops.pdp.tcp_port",
1393                         FT_UINT32, BASE_DEC, NULL, 0x0,
1394                          "TCP Port Number of PDP in PDPRedirAddr/LastPDPAddr object", HFILL }
1395                 },
1396                 { &hf_cops_accttimer,
1397                         { "Contents: ACCT Timer Value",           "cops.accttimer.value",
1398                         FT_UINT16, BASE_DEC, NULL, 0,
1399                         "Accounting Timer Value in AcctTimer object", HFILL }
1400                 },
1401                 { &hf_cops_key_id,
1402                         { "Contents: Key ID",           "cops.integrity.key_id",
1403                         FT_UINT32, BASE_DEC, NULL, 0,
1404                         "Key ID in Integrity object", HFILL }
1405                 },
1406                 { &hf_cops_seq_num,
1407                         { "Contents: Sequence Number",           "cops.integrity.seq_num",
1408                         FT_UINT32, BASE_DEC, NULL, 0,
1409                         "Sequence Number in Integrity object", HFILL }
1410                 },
1411                 { &hf_cops_gperror,
1412                         { "Error",           "cops.gperror",
1413                         FT_UINT16, BASE_DEC, VALS(cops_gperror_vals), 0,
1414                         "Error in Error object", HFILL }
1415                 },
1416                 { &hf_cops_gperror_sub,
1417                         { "Error Sub-code",           "cops.gperror_sub",
1418                         FT_UINT16, BASE_HEX, NULL, 0,
1419                         "Error Sub-code in Error object", HFILL }
1420                 },
1421                 { &hf_cops_cperror,
1422                         { "Error",           "cops.cperror",
1423                         FT_UINT16, BASE_DEC, VALS(cops_cperror_vals), 0,
1424                         "Error in Error object", HFILL }
1425                 },
1426                 { &hf_cops_cperror_sub,
1427                         { "Error Sub-code",           "cops.cperror_sub",
1428                         FT_UINT16, BASE_HEX, NULL, 0,
1429                         "Error Sub-code in Error object", HFILL }
1430                 },
1431
1432         };
1433
1434         /* Setup protocol subtree array */
1435         static gint *ett[] = {
1436                 &ett_cops,
1437                 &ett_cops_ver_flags,
1438                 &ett_cops_obj,
1439                 &ett_cops_pr_obj,
1440                 &ett_cops_obj_data,
1441                 &ett_cops_r_type_flags,
1442                 &ett_cops_itf,
1443                 &ett_cops_reason,
1444                 &ett_cops_decision,
1445                 &ett_cops_error,
1446                 &ett_cops_clientsi,
1447                 &ett_cops_asn1,
1448                 &ett_cops_gperror,
1449                 &ett_cops_cperror,
1450                 &ett_cops_pdp,
1451         };
1452
1453         module_t* cops_module;
1454
1455         /* Register the protocol name and description */
1456         proto_cops = proto_register_protocol("Common Open Policy Service",
1457             "COPS", "cops");
1458
1459         /* Required function calls to register the header fields and subtrees used */
1460         proto_register_field_array(proto_cops, hf, array_length(hf));
1461         proto_register_subtree_array(ett, array_length(ett));
1462
1463         /* Register our configuration options for cops,
1464          * particularly our ports
1465          */
1466         cops_module = prefs_register_protocol(proto_cops,
1467                                               proto_reg_handoff_cops);
1468         prefs_register_uint_preference(cops_module,"tcp.cops_port",
1469                                        "COPS TCP Port",
1470                                        "Set the TCP port for COPS messages",
1471                                        10,&global_cops_tcp_port);
1472         prefs_register_bool_preference(cops_module, "desegment",
1473             "Desegment all COPS messages spanning multiple TCP segments",
1474             "Whether the COPS dissector should desegment all messages spanning multiple TCP segments",
1475             &cops_desegment);
1476 };
1477
1478 void
1479 proto_reg_handoff_cops(void)
1480 {
1481         static int cops_prefs_initialized = FALSE;
1482         static dissector_handle_t cops_handle;
1483
1484         if (!cops_prefs_initialized) {
1485                 cops_handle = create_dissector_handle(dissect_cops, proto_cops);
1486                 cops_prefs_initialized = TRUE;
1487         } else
1488                 dissector_delete("tcp.port",cops_tcp_port,cops_handle);
1489
1490         /* Set our port numbers for future use */
1491         cops_tcp_port = global_cops_tcp_port;
1492
1493         dissector_add("tcp.port", cops_tcp_port, cops_handle);
1494 }