1 /* packet-sscop.c
2  * Routines for SSCOP (Q.2110, Q.SAAL) frame disassembly
3  * Guy Harris <guy@alum.mit.edu>
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998
8  *
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12 
13 #include "config.h"
14 
15 #include <epan/packet.h>
16 #include <epan/prefs.h>
17 #include <epan/proto_data.h>
18 
19 #include <wiretap/wtap.h>
20 #include "packet-sscop.h"
21 
22 void proto_register_sscop(void);
23 void proto_reg_handoff_sscop(void);
24 
25 int proto_sscop = -1;
26 
27 static int hf_sscop_type = -1;
28 static int hf_sscop_sq = -1;
29 static int hf_sscop_mr = -1;
30 static int hf_sscop_s = -1;
31 static int hf_sscop_ps = -1;
32 static int hf_sscop_r = -1;
33 static int hf_sscop_stat_s = -1;
34 static int hf_sscop_pad_length = -1;
35 static int hf_sscop_source = -1;
36 /* static int hf_sscop_stat_count = -1; */
37 
38 static gint ett_sscop = -1;
39 static gint ett_stat = -1;
40 
41 static dissector_handle_t q2931_handle;
42 static dissector_handle_t data_handle;
43 static dissector_handle_t sscf_nni_handle;
44 static dissector_handle_t alcap_handle;
45 static dissector_handle_t nbap_handle;
46 
47 static dissector_handle_t sscop_handle;
48 
49 
50 static const enum_val_t sscop_payload_dissector_options[] = {
51   { "data",     "Data (no further dissection)", DATA_DISSECTOR },
52   { "Q.2931",   "Q.2931",                       Q2931_DISSECTOR },
53   { "SSCF-NNI", "SSCF-NNI (MTP3-b)",            SSCF_NNI_DISSECTOR },
54   { "ALCAP",    "ALCAP",                        ALCAP_DISSECTOR },
55   { "NBAP",     "NBAP",                         NBAP_DISSECTOR },
56   { NULL,       NULL,                           0 }
57 };
58 
59 static guint sscop_payload_dissector = Q2931_DISSECTOR;
60 static dissector_handle_t default_handle;
61 
62 static sscop_info_t sscop_info;
63 /*
64  * See
65  *
66  *   http://web.archive.org/web/20150408122122/http://www.protocols.com/pbook/atmsig.htm
67  *
68  * for some information on SSCOP, although, alas, not the actual PDU
69  * type values - those I got from the FreeBSD 3.2 ATM code.
70  */
71 
72 /*
73  * SSCOP PDU types.
74  */
75 #define SSCOP_TYPE_MASK 0x0f
76 
77 #define SSCOP_BGN       0x01    /* Begin */
78 #define SSCOP_BGAK      0x02    /* Begin Acknowledge */
79 #define SSCOP_END       0x03    /* End */
80 #define SSCOP_ENDAK     0x04    /* End Acknowledge */
81 #define SSCOP_RS        0x05    /* Resynchronization */
82 #define SSCOP_RSAK      0x06    /* Resynchronization Acknowledge */
83 #define SSCOP_BGREJ     0x07    /* Begin Reject */
84 #define SSCOP_SD        0x08    /* Sequenced Data */
85 #if 0
86 #define SSCOP_SDP       0x09    /* Sequenced Data with Poll */
87 #endif
88 #define SSCOP_ER        0x09    /* Error Recovery */
89 #define SSCOP_POLL      0x0a    /* Status Request */
90 #define SSCOP_STAT      0x0b    /* Solicited Status Response */
91 #define SSCOP_USTAT     0x0c    /* Unsolicited Status Response */
92 #define SSCOP_UD        0x0d    /* Unnumbered Data */
93 #define SSCOP_MD        0x0e    /* Management Data */
94 #define SSCOP_ERAK      0x0f    /* Error Acknowledge */
95 
96 #define SSCOP_S         0x10    /* Source bit in End PDU */
97 
98 /*
99  * XXX - how to distinguish SDP from ER?
100  */
101 static const value_string sscop_type_vals[] = {
102   { SSCOP_BGN,   "Begin" },
103   { SSCOP_BGAK,  "Begin Acknowledge" },
104   { SSCOP_END,   "End" },
105   { SSCOP_ENDAK, "End Acknowledge" },
106   { SSCOP_RS,    "Resynchronization" },
107   { SSCOP_RSAK,  "Resynchronization Acknowledge" },
108   { SSCOP_BGREJ, "Begin Reject" },
109   { SSCOP_SD,    "Sequenced Data" },
110 #if 0
111   { SSCOP_SDP,   "Sequenced Data with Poll" },
112 #endif
113   { SSCOP_ER,    "Error Recovery" },
114   { SSCOP_POLL,  "Status Request" },
115   { SSCOP_STAT,  "Solicited Status Response" },
116   { SSCOP_USTAT, "Unsolicited Status Response" },
117   { SSCOP_UD,    "Unnumbered Data" },
118   { SSCOP_MD,    "Management Data" },
119   { SSCOP_ERAK,  "Error Acknowledge" },
120   { 0,            NULL }
121 };
122 static value_string_ext sscop_type_vals_ext = VALUE_STRING_EXT_INIT(sscop_type_vals);
123 
124 /*
125  * The SSCOP "header" is a trailer, so the "offsets" are computed based
126  * on the length of the packet.
127  */
128 
129 /*
130  * PDU type.
131  */
132 #define SSCOP_PDU_TYPE  (reported_length - 4)   /* single byte */
133 
134 /*
135  * Begin PDU, Begin Acknowledge PDU (no N(SQ) in it), Resynchronization
136  * PDU, Resynchronization Acknowledge PDU (no N(SQ) in it in Q.SAAL),
137  * Error Recovery PDU, Error Recovery Acknoledge PDU (no N(SQ) in it).
138  */
139 #define SSCOP_N_SQ      (reported_length - 5)   /* One byte */
140 #define SSCOP_N_MR      (reported_length - 4)   /* lower 3 bytes thereof */
141 
142 /*
143  * Sequenced Data PDU (no N(PS) in it), Sequenced Data with Poll PDU,
144  * Poll PDU.
145  */
146 #define SSCOP_N_PS      (reported_length - 8)   /* lower 3 bytes thereof */
147 #define SSCOP_N_S       (reported_length - 4)   /* lower 3 bytes thereof */
148 
149 /*
150  * Solicited Status PDU, Unsolicited Status PDU (no N(PS) in it).
151  */
152 #define SSCOP_SS_N_PS   (reported_length - 12)  /* lower 3 bytes thereof */
153 #define SSCOP_SS_N_MR   (reported_length - 8)   /* lower 3 bytes thereof */
154 #define SSCOP_SS_N_R    (reported_length - 4)   /* lower 3 bytes thereof */
155 
dissect_stat_list(proto_tree * tree,tvbuff_t * tvb,guint h)156 static void dissect_stat_list(proto_tree *tree, tvbuff_t *tvb,guint h) {
157   gint n,i;
158 
159   if ((n = (tvb_reported_length(tvb))/4 - h)) {
160     tree = proto_tree_add_subtree(tree,tvb,0,n*4,ett_stat,NULL,"SD List");
161 
162     for (i = 0; i < n; i++) {
163       proto_tree_add_item(tree, hf_sscop_stat_s, tvb, i*4 + 1,3,ENC_BIG_ENDIAN);
164     }
165   }
166 }
167 
168 extern void
dissect_sscop_and_payload(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,dissector_handle_t payload_handle)169 dissect_sscop_and_payload(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, dissector_handle_t payload_handle)
170 {
171   guint reported_length;
172   proto_item *ti;
173   proto_tree *sscop_tree = NULL;
174   guint8 sscop_pdu_type;
175   int pdu_len;
176   int pad_len;
177   tvbuff_t *next_tvb;
178 
179   reported_length = tvb_reported_length(tvb);   /* frame length */
180   sscop_pdu_type = tvb_get_guint8(tvb, SSCOP_PDU_TYPE);
181   sscop_info.type = sscop_pdu_type & SSCOP_TYPE_MASK;
182 
183   col_set_str(pinfo->cinfo, COL_PROTOCOL, "SSCOP");
184   col_add_str(pinfo->cinfo, COL_INFO, val_to_str_ext(sscop_info.type, &sscop_type_vals_ext,
185                                                  "Unknown PDU type (0x%02x)"));
186 
187   /*
188    * Find the length of the PDU and, if there's any payload and
189    * padding, the length of the padding.
190    */
191   switch (sscop_info.type) {
192 
193   case SSCOP_SD:
194     pad_len = (sscop_pdu_type >> 6) & 0x03;
195     pdu_len = 4;
196     break;
197 
198   case SSCOP_BGN:
199   case SSCOP_BGAK:
200   case SSCOP_BGREJ:
201   case SSCOP_END:
202   case SSCOP_RS:
203 #if 0
204   case SSCOP_SDP:
205 #endif
206     pad_len = (sscop_pdu_type >> 6) & 0x03;
207     sscop_info.payload_len = pdu_len = 8;
208     break;
209 
210   case SSCOP_UD:
211     pad_len = (sscop_pdu_type >> 6) & 0x03;
212     sscop_info.payload_len = pdu_len = 4;
213     break;
214 
215   default:
216     pad_len = 0;
217     pdu_len = reported_length;  /* No payload, just SSCOP */
218     sscop_info.payload_len = 0;
219     break;
220   }
221 
222   if (tree) {
223     ti = proto_tree_add_protocol_format(tree, proto_sscop, tvb,
224                                         reported_length - pdu_len,
225                                         pdu_len, "SSCOP");
226     sscop_tree = proto_item_add_subtree(ti, ett_sscop);
227 
228     proto_tree_add_item(sscop_tree, hf_sscop_type, tvb, SSCOP_PDU_TYPE, 1,ENC_BIG_ENDIAN);
229 
230     switch (sscop_info.type) {
231 
232     case SSCOP_BGN:
233     case SSCOP_RS:
234     case SSCOP_ER:
235       proto_tree_add_item(sscop_tree, hf_sscop_sq, tvb, SSCOP_N_SQ, 1,ENC_BIG_ENDIAN);
236       proto_tree_add_item(sscop_tree, hf_sscop_mr, tvb, SSCOP_N_MR + 1, 3, ENC_BIG_ENDIAN);
237       break;
238 
239     case SSCOP_END:
240       proto_tree_add_string(sscop_tree, hf_sscop_source, tvb, SSCOP_PDU_TYPE, 1,
241                                 (sscop_pdu_type & SSCOP_S) ? "SSCOP" : "User");
242       break;
243 
244     case SSCOP_BGAK:
245     case SSCOP_RSAK:
246       proto_tree_add_item(sscop_tree, hf_sscop_mr, tvb, SSCOP_N_MR + 1, 3, ENC_BIG_ENDIAN);
247       break;
248 
249     case SSCOP_ERAK:
250       proto_tree_add_item(sscop_tree, hf_sscop_mr, tvb, SSCOP_N_MR + 1, 3, ENC_BIG_ENDIAN);
251       break;
252 
253     case SSCOP_SD:
254       proto_tree_add_item(sscop_tree, hf_sscop_s, tvb, SSCOP_N_S + 1, 3, ENC_BIG_ENDIAN);
255       break;
256 
257 #if 0
258     case SSCOP_SDP:
259 #endif
260     case SSCOP_POLL:
261       proto_tree_add_item(sscop_tree, hf_sscop_ps, tvb, SSCOP_N_PS + 1, 3,ENC_BIG_ENDIAN);
262       proto_tree_add_item(sscop_tree, hf_sscop_s, tvb, SSCOP_N_S + 1, 3,ENC_BIG_ENDIAN);
263       break;
264 
265     case SSCOP_STAT:
266       proto_tree_add_item(sscop_tree, hf_sscop_ps, tvb, SSCOP_SS_N_PS + 1, 3,ENC_BIG_ENDIAN);
267       proto_tree_add_item(sscop_tree, hf_sscop_mr, tvb, SSCOP_SS_N_MR + 1, 3, ENC_BIG_ENDIAN);
268       proto_tree_add_item(sscop_tree, hf_sscop_r, tvb, SSCOP_SS_N_R + 1, 3,ENC_BIG_ENDIAN);
269       dissect_stat_list(sscop_tree,tvb,3);
270       break;
271 
272     case SSCOP_USTAT:
273       proto_tree_add_item(sscop_tree, hf_sscop_mr, tvb, SSCOP_SS_N_MR + 1, 3, ENC_BIG_ENDIAN);
274       proto_tree_add_item(sscop_tree, hf_sscop_r, tvb, SSCOP_SS_N_R + 1, 3,ENC_BIG_ENDIAN);
275       dissect_stat_list(sscop_tree,tvb,2);
276       break;
277     }
278   }
279 
280   /*
281    * Dissect the payload, if any.
282    *
283    * XXX - what about a Management Data PDU?
284    */
285   switch (sscop_info.type) {
286 
287   case SSCOP_SD:
288   case SSCOP_UD:
289   case SSCOP_BGN:
290   case SSCOP_BGAK:
291   case SSCOP_BGREJ:
292   case SSCOP_END:
293   case SSCOP_RS:
294 #if 0
295   case SSCOP_SDP:
296 #endif
297     if (tree) {
298       proto_tree_add_uint(sscop_tree, hf_sscop_pad_length, tvb, SSCOP_PDU_TYPE, 1, pad_len);
299     }
300 
301     /*
302      * Compute length of data in PDU - subtract the trailer length
303      * and the pad length from the reported length.
304      */
305     reported_length -= (pdu_len + pad_len);
306 
307     if (reported_length != 0) {
308       /*
309        * We know that we have all of the payload, because we know we have
310        * at least 4 bytes of data after the payload, i.e. the SSCOP trailer.
311        * Therefore, we know that the captured length of the payload is
312        * equal to the length of the payload.
313        */
314       next_tvb = tvb_new_subset_length(tvb, 0, reported_length);
315       if (sscop_info.type == SSCOP_SD)
316       {
317         call_dissector(payload_handle, next_tvb, pinfo, tree);
318       }
319     break;
320   }
321 }
322 }
323 
dissect_sscop(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data _U_)324 static int dissect_sscop(tvbuff_t* tvb, packet_info* pinfo,proto_tree* tree, void* data _U_)
325 {
326   struct _sscop_payload_info  *p_sscop_info;
327   dissector_handle_t subdissector;
328 
329   /* Look for packet info for subdissector information */
330   p_sscop_info = (struct _sscop_payload_info *)p_get_proto_data(wmem_file_scope(), pinfo, proto_sscop, 0);
331 
332   if ( p_sscop_info
333        && ( subdissector = p_sscop_info->subdissector )
334        && ( subdissector == data_handle
335             || subdissector == q2931_handle
336             || subdissector == sscf_nni_handle
337             || subdissector == alcap_handle
338             || subdissector == nbap_handle) )
339     dissect_sscop_and_payload(tvb,pinfo,tree,subdissector);
340   else
341     dissect_sscop_and_payload(tvb,pinfo,tree,default_handle);
342 
343   return tvb_captured_length(tvb);
344 }
345 
346 /* Make sure handles for various protocols are initialized */
initialize_handles_once(void)347 static void initialize_handles_once(void) {
348   static gboolean initialized = FALSE;
349   if (!initialized) {
350     q2931_handle = find_dissector("q2931");
351     data_handle = find_dissector("data");
352     sscf_nni_handle = find_dissector("sscf-nni");
353     alcap_handle = find_dissector("alcap");
354     nbap_handle = find_dissector("nbap");
355 
356     initialized = TRUE;
357   }
358 }
359 
sscop_allowed_subdissector(dissector_handle_t handle)360 gboolean sscop_allowed_subdissector(dissector_handle_t handle)
361 {
362   initialize_handles_once();
363   if (handle == q2931_handle || handle == data_handle
364       || handle == sscf_nni_handle || handle == alcap_handle
365       || handle == nbap_handle)
366     return TRUE;
367   return FALSE;
368 }
369 
370 void
proto_reg_handoff_sscop(void)371 proto_reg_handoff_sscop(void)
372 {
373   static gboolean prefs_initialized = FALSE;
374 
375   if (!prefs_initialized) {
376     initialize_handles_once();
377     dissector_add_uint_range_with_preference("udp.port", "", sscop_handle);
378     prefs_initialized = TRUE;
379   }
380 
381   dissector_add_uint("atm.aal5.type", TRAF_SSCOP, sscop_handle);
382 
383   switch(sscop_payload_dissector) {
384   case DATA_DISSECTOR:     default_handle = data_handle;     break;
385   case Q2931_DISSECTOR:    default_handle = q2931_handle;    break;
386   case SSCF_NNI_DISSECTOR: default_handle = sscf_nni_handle; break;
387   case ALCAP_DISSECTOR:    default_handle = alcap_handle;    break;
388   case NBAP_DISSECTOR:     default_handle = nbap_handle;     break;
389   }
390 
391 }
392 
393 void
proto_register_sscop(void)394 proto_register_sscop(void)
395 {
396   static hf_register_info hf[] = {
397     { &hf_sscop_type, { "PDU Type", "sscop.type", FT_UINT8, BASE_HEX|BASE_EXT_STRING, &sscop_type_vals_ext, SSCOP_TYPE_MASK, NULL, HFILL }},
398     { &hf_sscop_sq, { "N(SQ)", "sscop.sq", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
399     { &hf_sscop_mr, { "N(MR)", "sscop.mr", FT_UINT24, BASE_DEC, NULL, 0x0, NULL, HFILL }},
400     { &hf_sscop_s, { "N(S)", "sscop.s", FT_UINT24, BASE_DEC, NULL, 0x0, NULL, HFILL }},
401     { &hf_sscop_ps, { "N(PS)", "sscop.ps", FT_UINT24, BASE_DEC, NULL, 0x0, NULL, HFILL }},
402     { &hf_sscop_r, { "N(R)", "sscop.r", FT_UINT24, BASE_DEC, NULL, 0x0, NULL, HFILL }},
403     { &hf_sscop_stat_s, { "N(S)", "sscop.stat.s", FT_UINT24, BASE_DEC, NULL, 0x0,NULL, HFILL }},
404     { &hf_sscop_pad_length, { "Pad length", "sscop.pad_length", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
405     { &hf_sscop_source, { "Source", "sscop.source", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }},
406 #if 0
407     { &hf_sscop_stat_count, { "Number of NACKed pdus", "sscop.stat.count", FT_UINT32, BASE_DEC, NULL, 0x0,NULL, HFILL }}
408 #endif
409   };
410 
411   static gint *ett[] = {
412     &ett_sscop,
413     &ett_stat
414   };
415 
416   module_t *sscop_module;
417 
418   proto_sscop = proto_register_protocol("SSCOP", "SSCOP", "sscop");
419   proto_register_field_array(proto_sscop, hf, array_length(hf));
420   proto_register_subtree_array(ett, array_length(ett));
421 
422   sscop_handle = register_dissector("sscop", dissect_sscop, proto_sscop);
423 
424   sscop_module = prefs_register_protocol(proto_sscop, proto_reg_handoff_sscop);
425 
426   prefs_register_enum_preference(sscop_module, "payload",
427                                  "SSCOP payload protocol",
428                                  "SSCOP payload (dissector to call on SSCOP payload)",
429                                  (gint *)&sscop_payload_dissector,
430                                  sscop_payload_dissector_options, FALSE);
431 }
432 
433 /*
434  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
435  *
436  * Local Variables:
437  * c-basic-offset: 2
438  * tab-width: 8
439  * indent-tabs-mode: nil
440  * End:
441  *
442  * ex: set shiftwidth=2 tabstop=8 expandtab:
443  * :indentSize=2:tabSize=8:noTabs=true:
444  */
445