Don't let the user specify a maximum capture file size if they're not
[obnox/wireshark/wip.git] / packet-sctp.c
1 /* packet-sctp.c
2  * Routines for Stream Control Transmission Protocol dissection
3  * It should be compilant to
4  * - RFC 2960, for basic SCTP support
5  * - http://www.ietf.org/internet-drafts/draft-ietf-tsvwg-addip-sctp-03.txt for the add-IP extension
6  * - http://www.sctp.org/draft-ietf-tsvwg-usctp-01.txt for the 'Limited Retransmission' extension
7  * Copyright 2000, Michael Tuexen <Michael.Tuexen@icn.siemens.de>
8  * Still to do (so stay tuned)
9  * - support for reassembly
10  * - support for new checksum
11  * - code cleanup
12  *
13  * $Id: packet-sctp.c,v 1.25 2001/12/19 20:01:56 guy Exp $
14  *
15  * Ethereal - Network traffic analyzer
16  * By Gerald Combs <gerald@ethereal.com>
17  * Copyright 1998 Gerald Combs
18  *
19  * Copied from README.developer
20  * 
21  * This program is free software; you can redistribute it and/or
22  * modify it under the terms of the GNU General Public License
23  * as published by the Free Software Foundation; either version 2
24  * of the License, or (at your option) any later version.
25  * 
26  * This program is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  * GNU General Public License for more details.
30  * 
31  * You should have received a copy of the GNU General Public License
32  * along with this program; if not, write to the Free Software
33  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34  */
35
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39
40 #include <stdio.h>
41 #include <stdlib.h>
42
43
44 #ifdef HAVE_SYS_TYPES_H
45 # include <sys/types.h>
46 #endif
47
48 #ifdef HAVE_NETINET_IN_H
49 # include <netinet/in.h>
50 #endif
51
52 #include <string.h>
53 #include <glib.h>
54
55 #ifdef NEED_SNPRINTF_H
56 # include "snprintf.h"
57 #endif
58
59 #include "packet.h"
60 #include "ipproto.h"
61
62 /* Initialize the protocol and registered fields */
63 static int proto_sctp = -1;
64 static int hf_sctp_source_port      = -1;
65 static int hf_sctp_destination_port = -1;
66 static int hf_sctp_verification_tag = -1;
67 static int hf_sctp_checksum         = -1;
68 static int hf_sctp_checksum_correct = -1;
69
70 static int hf_sctp_chunk_type       = -1;
71 static int hf_sctp_chunk_flags      = -1;
72 static int hf_sctp_chunk_length     = -1;
73
74 static int hf_sctp_init_chunk_initiate_tag   = -1;
75 static int hf_sctp_init_chunk_adv_rec_window_credit = -1;
76 static int hf_sctp_init_chunk_number_of_outbound_streams = -1;
77 static int hf_sctp_init_chunk_number_of_inbound_streams  = -1;
78 static int hf_sctp_init_chunk_initial_tsn    = -1;
79
80 static int hf_sctp_cumulative_tsn_ack = -1;
81
82 static int hf_sctp_data_chunk_tsn = -1;
83 static int hf_sctp_data_chunk_stream_id = -1;
84 static int hf_sctp_data_chunk_stream_seq_number = -1;
85 static int hf_sctp_data_chunk_payload_proto_id = -1;
86
87 static int hf_sctp_data_chunk_e_bit = -1;
88 static int hf_sctp_data_chunk_b_bit = -1;
89 static int hf_sctp_data_chunk_u_bit = -1;
90
91 static int hf_sctp_sack_chunk_cumulative_tsn_ack = -1;
92 static int hf_sctp_sack_chunk_adv_rec_window_credit = -1;
93 static int hf_sctp_sack_chunk_number_of_gap_blocks = -1;
94 static int hf_sctp_sack_chunk_number_of_dup_tsns = -1;
95 static int hf_sctp_sack_chunk_gap_block_start = -1;
96 static int hf_sctp_sack_chunk_gap_block_end = -1;
97 static int hf_sctp_sack_chunk_duplicate_tsn = -1;
98
99 static int hf_sctp_shutdown_chunk_cumulative_tsn_ack = -1;
100
101 static int hf_sctp_cwr_chunk_lowest_tsn = -1;
102
103 static int hf_sctp_ecne_chunk_lowest_tsn = -1;
104
105 static int hf_sctp_shutdown_complete_chunk_t_bit = -1;
106
107 static int hf_sctp_chunk_parameter_type = -1;
108 static int hf_sctp_chunk_parameter_length = -1;
109 static int hf_sctp_parameter_ipv4_address = -1;
110 static int hf_sctp_parameter_ipv6_address = -1;
111 static int hf_sctp_parameter_cookie_preservative_increment = -1;
112 static int hf_sctp_parameter_hostname_hostname = -1;
113 static int hf_sctp_supported_address_types_parameter = -1;
114
115 static int hf_sctp_cause_code = -1;
116 static int hf_sctp_cause_length = -1;
117 static int hf_sctp_cause_stream_identifier = -1;
118
119 static int hf_sctp_cause_number_of_missing_parameters = -1;
120 static int hf_sctp_cause_missing_parameter_type = -1;
121
122 static int hf_sctp_cause_measure_of_staleness = -1;
123
124 static int hf_sctp_cause_tsn = -1;
125
126 static int hf_sctp_forward_tsn_chunk_tsn = -1;
127
128 static int hf_sctp_ustreams_start = -1;
129 static int hf_sctp_ustreams_end   = -1;
130
131 static int hf_sctp_asconf_ack_serial = -1;
132 static int hf_sctp_asconf_ack_correlation_id = -1;
133
134 static int hf_sctp_asconf_serial = -1;
135 static int hf_sctp_asconf_correlation_id = -1;
136 static int hf_sctp_asconf_reserved = -1;
137 static int hf_sctp_asconf_addr_type = -1;
138 static int hf_sctp_asconf_addr = -1;
139 static int hf_sctp_adap_indication = -1;
140
141 static dissector_table_t sctp_port_dissector_table;
142 static dissector_table_t sctp_ppi_dissector_table;
143
144 /* Initialize the subtree pointers */
145 static gint ett_sctp = -1;
146 static gint ett_sctp_chunk = -1;
147 static gint ett_sctp_chunk_parameter = -1;
148 static gint ett_sctp_chunk_cause = -1;
149 static gint ett_sctp_data_chunk_flags = -1;
150 static gint ett_sctp_sack_chunk_gap_block = -1;
151 static gint ett_sctp_supported_address_types_parameter = -1;
152 static gint ett_sctp_unrecognized_parameter_parameter = -1;
153 static gint ett_sctp_unreliable_streams_interval = -1;
154
155 #define SCTP_DATA_CHUNK_ID               0
156 #define SCTP_INIT_CHUNK_ID               1
157 #define SCTP_INIT_ACK_CHUNK_ID           2
158 #define SCTP_SACK_CHUNK_ID               3
159 #define SCTP_HEARTBEAT_CHUNK_ID          4
160 #define SCTP_HEARTBEAT_ACK_CHUNK_ID      5
161 #define SCTP_ABORT_CHUNK_ID              6
162 #define SCTP_SHUTDOWN_CHUNK_ID           7
163 #define SCTP_SHUTDOWN_ACK_CHUNK_ID       8
164 #define SCTP_ERROR_CHUNK_ID              9
165 #define SCTP_COOKIE_ECHO_CHUNK_ID       10
166 #define SCTP_COOKIE_ACK_CHUNK_ID        11
167 #define SCTP_ECNE_CHUNK_ID              12
168 #define SCTP_CWR_CHUNK_ID               13
169 #define SCTP_SHUTDOWN_COMPLETE_CHUNK_ID 14
170 #define SCTP_FORWARD_TSN_CHUNK_ID      192 
171 #define SCTP_ASCONF_ACK_CHUNK_ID      0x80
172 #define SCTP_ASCONF_CHUNK_ID          0XC1
173
174 #define SCTP_IETF_EXT                  255
175
176 static const value_string sctp_chunk_type_values[] = {
177   { SCTP_DATA_CHUNK_ID,              "DATA" },
178   { SCTP_INIT_CHUNK_ID,              "INIT" },
179   { SCTP_INIT_ACK_CHUNK_ID,          "INIT_ACK" },
180   { SCTP_SACK_CHUNK_ID,              "SACK" },
181   { SCTP_HEARTBEAT_CHUNK_ID,         "HEARTBEAT" },
182   { SCTP_HEARTBEAT_ACK_CHUNK_ID,     "HEARTBEAT_ACK" },
183   { SCTP_ABORT_CHUNK_ID,             "ABORT" },
184   { SCTP_SHUTDOWN_CHUNK_ID,          "SHUTDOWN" },
185   { SCTP_SHUTDOWN_ACK_CHUNK_ID,      "SHUTDOWN_ACK" },
186   { SCTP_ERROR_CHUNK_ID,             "ERROR" },
187   { SCTP_COOKIE_ECHO_CHUNK_ID,       "COOKIE_ECHO" },
188   { SCTP_COOKIE_ACK_CHUNK_ID,        "COOKIE_ACK" },
189   { SCTP_ECNE_CHUNK_ID,              "ECNE" },
190   { SCTP_CWR_CHUNK_ID,               "CWR" },
191   { SCTP_SHUTDOWN_COMPLETE_CHUNK_ID, "SHUTDOWN_COMPLETE" },
192   { SCTP_FORWARD_TSN_CHUNK_ID,       "FORWARD TSN" },
193   { SCTP_ASCONF_ACK_CHUNK_ID,        "ASCONF_ACK" },
194   { SCTP_ASCONF_CHUNK_ID,            "ASCONF" },
195   { SCTP_IETF_EXT,                   "IETF_EXTENSION" },
196   { 0,                               NULL } };
197
198 #define HEARTBEAT_INFO_PARAMETER_ID          0x0001
199 #define IPV4ADDRESS_PARAMETER_ID             0x0005
200 #define IPV6ADDRESS_PARAMETER_ID             0x0006
201 #define STATE_COOKIE_PARAMETER_ID            0x0007
202 #define UNREC_PARA_PARAMETER_ID              0x0008
203 #define COOKIE_PRESERVATIVE_PARAMETER_ID     0x0009
204 #define HOSTNAME_ADDRESS_PARAMETER_ID        0x000b
205 #define SUPPORTED_ADDRESS_TYPES_PARAMETER_ID 0x000c
206 #define ECN_PARAMETER_ID                     0x8000
207 #define UNRELIABLE_STREAMS_PARAMETER_ID      0xC000
208 #define ADD_IP_ADDRESS_PARAMETER_ID          0xC001
209 #define DEL_IP_ADDRESS_PARAMETER_ID          0xC002
210 #define ERROR_CAUSE_INDICATION_PARAMETER_ID  0xC003
211 #define SET_PRIMARY_ADDRESS_PARAMETER_ID     0xC004
212 #define SUCCESS_REPORT_PARAMETER_ID          0xC005
213 #define ADAP_LAYER_INDICATION_PARAMETER_ID   0xC006
214
215 static const value_string sctp_parameter_identifier_values[] = {
216   { HEARTBEAT_INFO_PARAMETER_ID,          "Heartbeat info" },
217   { IPV4ADDRESS_PARAMETER_ID,             "IPv4 address" },
218   { IPV6ADDRESS_PARAMETER_ID,             "IPv6 address" },
219   { STATE_COOKIE_PARAMETER_ID,            "State cookie" },
220   { UNREC_PARA_PARAMETER_ID,              "Unrecognized parameters" },
221   { COOKIE_PRESERVATIVE_PARAMETER_ID,     "Cookie preservative" },
222   { HOSTNAME_ADDRESS_PARAMETER_ID,        "Hostname address" },
223   { SUPPORTED_ADDRESS_TYPES_PARAMETER_ID, "Supported address types" },
224   { ECN_PARAMETER_ID,                     "ECN" },
225   { UNRELIABLE_STREAMS_PARAMETER_ID,      "Unreliable streams" },
226   { ADD_IP_ADDRESS_PARAMETER_ID,          "Add IP address" },
227   { DEL_IP_ADDRESS_PARAMETER_ID,          "Delete IP address" },
228   { ERROR_CAUSE_INDICATION_PARAMETER_ID,  "Error cause indication" },
229   { SET_PRIMARY_ADDRESS_PARAMETER_ID,     "Set primary address" },
230   { SUCCESS_REPORT_PARAMETER_ID,          "Success report" },
231   { ADAP_LAYER_INDICATION_PARAMETER_ID,   "Adaptation Layer Indication" },
232   { 0,                                    NULL } };
233
234 #define PARAMETER_TYPE_LENGTH            2
235 #define PARAMETER_LENGTH_LENGTH          2
236 #define PARAMETER_HEADER_LENGTH          (PARAMETER_TYPE_LENGTH + PARAMETER_LENGTH_LENGTH)
237
238 #define PARAMETER_HEADER_OFFSET          0
239 #define PARAMETER_TYPE_OFFSET            PARAMETER_HEADER_OFFSET
240 #define PARAMETER_LENGTH_OFFSET          (PARAMETER_TYPE_OFFSET + PARAMETER_TYPE_LENGTH)
241 #define PARAMETER_VALUE_OFFSET           (PARAMETER_LENGTH_OFFSET + PARAMETER_LENGTH_LENGTH)
242
243 #define HEARTBEAT_INFO_PARAMETER_INFO_OFFSET PARAMETER_VALUE_OFFSET
244 #define HEARTBEAT_INFO_PARAMETER_HEADER_LENGTH PARAMETER_HEADER_LENGTH
245
246 #define IPV4_ADDRESS_LENGTH              4
247 #define IPV6_ADDRESS_LENGTH              16
248
249 #define STATE_COOKIE_PARAMETER_HEADER_LENGTH   PARAMETER_HEADER_LENGTH
250 #define STATE_COOKIE_PARAMETER_COOKIE_OFFSET   PARAMETER_VALUE_OFFSET
251
252 #define COOKIE_PRESERVATIVE_PARAMETER_INCR_OFFSET PARAMETER_VALUE_OFFSET
253 #define COOKIE_PRESERVATIVE_PARAMETER_INCR_LENGTH 4
254 #define SUPPORTED_ADDRESS_TYPE_PARAMETER_ADDRESS_TYPE_LENGTH 2
255
256 #define CAUSE_CODE_LENGTH            2
257 #define CAUSE_LENGTH_LENGTH          2
258 #define CAUSE_HEADER_LENGTH          (CAUSE_CODE_LENGTH + CAUSE_LENGTH_LENGTH)
259
260 #define CAUSE_HEADER_OFFSET          0
261 #define CAUSE_CODE_OFFSET            CAUSE_HEADER_OFFSET
262 #define CAUSE_LENGTH_OFFSET          (CAUSE_CODE_OFFSET + CAUSE_CODE_LENGTH)
263 #define CAUSE_INFO_OFFSET            (CAUSE_LENGTH_OFFSET + CAUSE_LENGTH_LENGTH)
264
265 #define CAUSE_STREAM_IDENTIFIER_LENGTH 2
266 #define CAUSE_RESERVED_LENGTH 2
267 #define CAUSE_STREAM_IDENTIFIER_OFFSET CAUSE_INFO_OFFSET
268 #define CAUSE_RESERVED_OFFSET          (CAUSE_STREAM_IDENTIFIER_OFFSET + CAUSE_STREAM_IDENTIFIER_LENGTH)
269
270 #define CAUSE_NUMBER_OF_MISSING_PARAMETERS_LENGTH 4
271 #define CAUSE_MISSING_PARAMETER_TYPE_LENGTH       2
272
273 #define CAUSE_NUMBER_OF_MISSING_PARAMETERS_OFFSET CAUSE_INFO_OFFSET
274 #define CAUSE_FIRST_MISSING_PARAMETER_TYPE_OFFSET (CAUSE_NUMBER_OF_MISSING_PARAMETERS_OFFSET + \
275                                                    CAUSE_NUMBER_OF_MISSING_PARAMETERS_LENGTH )
276
277 #define CAUSE_MEASURE_OF_STALENESS_LENGTH 4
278 #define CAUSE_MEASURE_OF_STALENESS_OFFSET CAUSE_INFO_OFFSET
279
280 #define CAUSE_TSN_LENGTH 4
281 #define CAUSE_TSN_OFFSET CAUSE_INFO_OFFSET
282
283 #define INVALID_STREAM_IDENTIFIER                  0x01
284 #define MISSING_MANDATORY_PARAMETERS               0x02
285 #define STALE_COOKIE_ERROR                         0x03
286 #define OUT_OF_RESOURCE                            0x04
287 #define UNRESOLVABLE_ADDRESS                       0x05
288 #define UNRECOGNIZED_CHUNK_TYPE                    0x06
289 #define INVALID_MANDATORY_PARAMETER                0x07
290 #define UNRECOGNIZED_PARAMETERS                    0x08
291 #define NO_USER_DATA                               0x09
292 #define COOKIE_RECEIVED_WHILE_SHUTTING_DOWN        0x0a
293 #define REQUEST_TO_DELETE_LAST_ADDRESS             0x0c
294 #define OPERATION_REFUSED_DUE_TO_RESOURCE_SHORTAGE 0X0d
295 #define REQUEST_TO_DELETE_SOURCE_ADDRESS           0x0e
296
297 static const value_string sctp_cause_code_values[] = {
298   { INVALID_STREAM_IDENTIFIER,                  "Invalid stream identifier" },
299   { MISSING_MANDATORY_PARAMETERS,               "Missing mandator parameter" },
300   { STALE_COOKIE_ERROR,                         "Stale cookie error" },
301   { OUT_OF_RESOURCE,                            "Out of resource" },
302   { UNRESOLVABLE_ADDRESS,                       "Unresolvable address" },
303   { UNRECOGNIZED_CHUNK_TYPE,                    "Unrecognized chunk type " },
304   { INVALID_MANDATORY_PARAMETER,                "Invalid mandatory parameter" },
305   { UNRECOGNIZED_PARAMETERS,                    "Unrecognized parameters" },
306   { NO_USER_DATA,                               "No user data" },
307   { COOKIE_RECEIVED_WHILE_SHUTTING_DOWN,        "Cookie received while shutting down" },
308   { REQUEST_TO_DELETE_LAST_ADDRESS,             "Request to delete last address" },
309   { OPERATION_REFUSED_DUE_TO_RESOURCE_SHORTAGE, "Operation refused due to resource shortage" },
310   { REQUEST_TO_DELETE_SOURCE_ADDRESS,           "Request to delete source address" },
311   { 0,                                          NULL } };
312
313 #define NOT_SPECIFIED_PROTOCOL_ID  0
314 #define IUA_PAYLOAD_PROTOCOL_ID    1
315 #define M2UA_PAYLOAD_PROTOCOL_ID   2
316 #define M3UA_PAYLOAD_PROTOCOL_ID   3
317 #define SUA_PAYLOAD_PROTOCOL_ID    4
318 #define M2PA_PAYLOAD_PROTOCOL_ID   5
319 #define V5UA_PAYLOAD_PROTOCOL_ID   6
320
321 static const value_string sctp_payload_proto_id_values[] = {
322   { NOT_SPECIFIED_PROTOCOL_ID,           "not specified" },
323   { IUA_PAYLOAD_PROTOCOL_ID,             "IUA" },
324   { M2UA_PAYLOAD_PROTOCOL_ID,            "M2UA" },
325   { M3UA_PAYLOAD_PROTOCOL_ID,            "M3UA" },
326   { SUA_PAYLOAD_PROTOCOL_ID,             "SUA" },
327   { M2PA_PAYLOAD_PROTOCOL_ID,            "M2PA" },
328   { V5UA_PAYLOAD_PROTOCOL_ID,            "V5UA" },
329   { 0,                                   NULL } };
330
331 /* The structure of the common header is described by the following constants */
332 #define SOURCE_PORT_LENGTH      2
333 #define DESTINATION_PORT_LENGTH 2
334 #define VERIFICATION_TAG_LENGTH 4
335 #define CHECKSUM_LENGTH         4
336 #define COMMON_HEADER_LENGTH    (SOURCE_PORT_LENGTH + \
337                                  DESTINATION_PORT_LENGTH + \
338                                  VERIFICATION_TAG_LENGTH + \
339                                  CHECKSUM_LENGTH)
340 #define SOURCE_PORT_OFFSET      0
341 #define DESTINATION_PORT_OFFSET (SOURCE_PORT_OFFSET + SOURCE_PORT_LENGTH)
342 #define VERIFICATION_TAG_OFFSET (DESTINATION_PORT_OFFSET + DESTINATION_PORT_LENGTH)
343 #define CHECKSUM_OFFSET         (VERIFICATION_TAG_OFFSET + VERIFICATION_TAG_LENGTH)
344
345 /* The structure of the chunk header is described by the following constants */
346 #define CHUNK_TYPE_LENGTH             1
347 #define CHUNK_FLAGS_LENGTH            1
348 #define CHUNK_LENGTH_LENGTH           2
349 #define CHUNK_HEADER_LENGTH           (CHUNK_TYPE_LENGTH + \
350                                        CHUNK_FLAGS_LENGTH + \
351                                        CHUNK_LENGTH_LENGTH)
352 #define CHUNK_HEADER_OFFSET           0
353 #define CHUNK_TYPE_OFFSET             CHUNK_HEADER_OFFSET
354 #define CHUNK_FLAGS_OFFSET            (CHUNK_TYPE_OFFSET + CHUNK_TYPE_LENGTH)
355 #define CHUNK_LENGTH_OFFSET           (CHUNK_FLAGS_OFFSET + CHUNK_FLAGS_LENGTH)
356 #define CHUNK_VALUE_OFFSET            (CHUNK_LENGTH_OFFSET + CHUNK_LENGTH_LENGTH)
357
358 /* The following constants describe the structure of DATA chunks */
359 #define DATA_CHUNK_TSN_LENGTH         4
360 #define DATA_CHUNK_STREAM_ID_LENGTH   2
361 #define DATA_CHUNK_STREAM_SEQ_NUMBER_LENGTH 2
362 #define DATA_CHUNK_PAYLOAD_PROTOCOL_ID_LENGTH 4
363
364 #define DATA_CHUNK_TSN_OFFSET         (CHUNK_VALUE_OFFSET + 0)
365 #define DATA_CHUNK_STREAM_ID_OFFSET   (DATA_CHUNK_TSN_OFFSET + DATA_CHUNK_TSN_LENGTH)
366 #define DATA_CHUNK_STREAM_SEQ_NUMBER_OFFSET (DATA_CHUNK_STREAM_ID_OFFSET + \
367                                              DATA_CHUNK_STREAM_SEQ_NUMBER_LENGTH)
368 #define DATA_CHUNK_PAYLOAD_PROTOCOL_ID_OFFSET (DATA_CHUNK_STREAM_SEQ_NUMBER_OFFSET + \
369                                                DATA_CHUNK_STREAM_SEQ_NUMBER_LENGTH)
370 #define DATA_CHUNK_PAYLOAD_OFFSET     (DATA_CHUNK_PAYLOAD_PROTOCOL_ID_OFFSET + \
371                                        DATA_CHUNK_PAYLOAD_PROTOCOL_ID_LENGTH)
372
373 #define DATA_CHUNK_HEADER_LENGTH      (CHUNK_HEADER_LENGTH + \
374                                        DATA_CHUNK_TSN_LENGTH + \
375                                        DATA_CHUNK_STREAM_ID_LENGTH + \
376                                        DATA_CHUNK_STREAM_SEQ_NUMBER_LENGTH + \
377                                        DATA_CHUNK_PAYLOAD_PROTOCOL_ID_LENGTH)
378
379 #define SCTP_DATA_CHUNK_E_BIT 0x01
380 #define SCTP_DATA_CHUNK_B_BIT 0x02
381 #define SCTP_DATA_CHUNK_U_BIT 0x04
382
383 #define INIT_CHUNK_INITIATE_TAG_LENGTH               4
384 #define INIT_CHUNK_ADV_REC_WINDOW_CREDIT_LENGTH      4
385 #define INIT_CHUNK_NUMBER_OF_OUTBOUND_STREAMS_LENGTH 2
386 #define INIT_CHUNK_NUMBER_OF_INBOUND_STREAMS_LENGTH  2
387 #define INIT_CHUNK_INITIAL_TSN_LENGTH                4
388
389 #define INIT_CHUNK_INITIATE_TAG_OFFSET               CHUNK_VALUE_OFFSET
390 #define INIT_CHUNK_ADV_REC_WINDOW_CREDIT_OFFSET      (INIT_CHUNK_INITIATE_TAG_OFFSET + \
391                                                       INIT_CHUNK_INITIATE_TAG_LENGTH )
392 #define INIT_CHUNK_NUMBER_OF_OUTBOUND_STREAMS_OFFSET (INIT_CHUNK_ADV_REC_WINDOW_CREDIT_OFFSET + \
393                                                       INIT_CHUNK_ADV_REC_WINDOW_CREDIT_LENGTH )
394 #define INIT_CHUNK_NUMBER_OF_INBOUND_STREAMS_OFFSET  (INIT_CHUNK_NUMBER_OF_OUTBOUND_STREAMS_OFFSET + \
395                                                       INIT_CHUNK_NUMBER_OF_OUTBOUND_STREAMS_LENGTH )
396 #define INIT_CHUNK_INITIAL_TSN_OFFSET                (INIT_CHUNK_NUMBER_OF_INBOUND_STREAMS_OFFSET + \
397                                                       INIT_CHUNK_NUMBER_OF_INBOUND_STREAMS_LENGTH )
398 #define INIT_CHUNK_VARIABLE_LENGTH_PARAMETER_OFFSET  (INIT_CHUNK_INITIAL_TSN_OFFSET + \
399                                                       INIT_CHUNK_INITIAL_TSN_LENGTH )
400
401
402 #define SACK_CHUNK_CUMULATIVE_TSN_ACK_LENGTH    4
403 #define SACK_CHUNK_ADV_REC_WINDOW_CREDIT_LENGTH 4
404 #define SACK_CHUNK_NUMBER_OF_GAP_BLOCKS_LENGTH  2
405 #define SACK_CHUNK_NUMBER_OF_DUP_TSNS_LENGTH    2
406 #define SACK_CHUNK_GAP_BLOCK_LENGTH             4
407 #define SACK_CHUNK_GAP_BLOCK_START_LENGTH       2
408 #define SACK_CHUNK_GAP_BLOCK_END_LENGTH         2
409 #define SACK_CHUNK_DUP_TSN_LENGTH               4
410
411 #define SACK_CHUNK_CUMULATIVE_TSN_ACK_OFFSET (CHUNK_VALUE_OFFSET + 0)
412 #define SACK_CHUNK_ADV_REC_WINDOW_CREDIT_OFFSET (SACK_CHUNK_CUMULATIVE_TSN_ACK_OFFSET + \
413                                                  SACK_CHUNK_CUMULATIVE_TSN_ACK_LENGTH)
414 #define SACK_CHUNK_NUMBER_OF_GAP_BLOCKS_OFFSET (SACK_CHUNK_ADV_REC_WINDOW_CREDIT_OFFSET + \
415                                                 SACK_CHUNK_ADV_REC_WINDOW_CREDIT_LENGTH)
416 #define SACK_CHUNK_NUMBER_OF_DUP_TSNS_OFFSET (SACK_CHUNK_NUMBER_OF_GAP_BLOCKS_OFFSET + \
417                                               SACK_CHUNK_NUMBER_OF_GAP_BLOCKS_LENGTH)
418 #define SACK_CHUNK_GAP_BLOCK_OFFSET (SACK_CHUNK_NUMBER_OF_DUP_TSNS_OFFSET + \
419                                      SACK_CHUNK_NUMBER_OF_DUP_TSNS_LENGTH)
420
421 #define HEARTBEAT_CHUNK_INFO_OFFSET CHUNK_VALUE_OFFSET
422
423 #define SHUTDOWN_CHUNK_CUMULATIVE_TSN_ACK_OFFSET CHUNK_VALUE_OFFSET
424 #define SHUTDOWN_CHUNK_CUMULATIVE_TSN_ACK_LENGTH 4
425                 
426 #define ABORT_CHUNK_FIRST_ERROR_CAUSE_OFFSET 4     
427 #define ERROR_CHUNK_FIRST_ERROR_CAUSE_OFFSET 4     
428
429 #define COOKIE_ECHO_CHUNK_COOKIE_OFFSET CHUNK_VALUE_OFFSET
430
431 #define ECNE_CHUNK_LOWEST_TSN_OFFSET CHUNK_VALUE_OFFSET
432 #define ECNE_CHUNK_LOWEST_TSN_LENGTH 4
433
434 #define CWR_CHUNK_LOWEST_TSN_OFFSET CHUNK_VALUE_OFFSET
435 #define CWR_CHUNK_LOWEST_TSN_LENGTH 4
436                      
437 #define SCTP_SHUTDOWN_COMPLETE_CHUNK_T_BIT 0x01
438
439 static const true_false_string sctp_data_chunk_e_bit_value = {
440   "Last segment",
441   "Not the last segment"
442 };
443
444 static const true_false_string sctp_data_chunk_b_bit_value = {
445   "First segment",
446   "Subsequent segment"
447 };
448
449 static const true_false_string sctp_data_chunk_u_bit_value = {
450   "Unordered delivery",
451   "Ordered deliviery"
452 };
453
454 static const true_false_string sctp_shutdown_complete_chunk_t_bit_value = {
455   "No TCB destroyed",
456   "TCB destroyed"
457 };
458
459 /* adler32.c -- compute the Adler-32 checksum of a data stream
460  * Copyright (C) 1995-1996 Mark Adler
461  * For conditions of distribution and use, see copyright notice in zlib.h
462  * available, e.g. from  http://www.cdrom.com/pub/infozip/zlib/
463  *
464  * It was modified for the use in this dissector.
465  */
466
467 #define BASE 65521L /* largest prime smaller than 65536      */
468 #define NMAX 5540   /* NMAX is the largest n - 12 such that  */
469                     /* 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
470
471 #define DO1(buf,i)  {s1 += buf[i]; s2 += s1;}
472 #define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);
473 #define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);
474 #define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);
475 #define DO16(buf)   DO8(buf,0); DO8(buf,8);
476
477 static unsigned int
478 sctp_adler_32(const unsigned char* buf,
479               unsigned int len)
480 {
481     unsigned int s1 = 1L;
482     unsigned int s2 = 0L;
483     int k;
484     
485     /* handle the first 8 bytes of the datagram */
486     DO8(buf,0);
487     buf += SOURCE_PORT_LENGTH + 
488            DESTINATION_PORT_LENGTH + 
489            VERIFICATION_TAG_LENGTH;
490     
491     /* handle four 0 bytes as checksum */
492     s2  += CHECKSUM_LENGTH * s1;
493     buf += CHECKSUM_LENGTH;
494
495     /* now we have 12 bytes handled */
496     len -= COMMON_HEADER_LENGTH;
497
498     /* handle the rest of the datagram */
499     while (len > 0) {
500         k = len < NMAX ? len : NMAX;
501         len -= k;
502         while (k >= 16) {
503             DO16(buf);
504             buf += 16;
505             k -= 16;
506         }
507         if (k != 0) do {
508             s1 += *buf++;
509             s2 += s1;
510         } while (--k);
511         s1 %= BASE;
512         s2 %= BASE;
513     }
514     return (s2 << 16) | s1;
515 }
516
517 static char 
518 *sctp_checksum_state(tvbuff_t *tvb, guint orig_checksum)
519 {
520   guint length;
521   
522   length = tvb_length(tvb);
523   if (orig_checksum == sctp_adler_32(tvb_get_ptr(tvb, 0, length), length))
524     return "correct";
525   else
526     return "incorrect";
527 }
528
529 static gboolean 
530 sctp_checksum_correct(tvbuff_t *tvb, guint orig_checksum)
531 {
532   guint length;
533   
534   length = tvb_length(tvb);
535   if (orig_checksum == sctp_adler_32(tvb_get_ptr(tvb, 0, length), length))
536     return TRUE;
537   else
538     return FALSE;
539 }
540
541 static guint 
542 nr_of_padding_bytes (guint length)
543 {
544   guint remainder;
545
546   remainder = length % 4;
547
548   if (remainder == 0)
549     return 0;
550   else
551     return 4 - remainder;
552 }
553
554 /* 
555  * TLV parameter stuff for INIT and INIT-ACK chunks
556  */
557
558 static void
559 dissect_parameter(tvbuff_t *, packet_info *, proto_tree *);
560
561 static void
562 dissect_error_cause(tvbuff_t *cause_tvb, packet_info *pinfo, proto_tree *chunk_tree);
563
564 static gboolean
565 dissect_sctp_chunk(tvbuff_t *, packet_info *, proto_tree *, proto_tree *);
566
567 static void 
568 dissect_tlv_parameter_list(tvbuff_t *parameter_list_tvb, packet_info *pinfo, proto_tree *tree)
569 {
570   guint offset, length, padding_length, total_length;
571   tvbuff_t *parameter_tvb;
572
573   offset = 0;
574   while(tvb_length_remaining(parameter_list_tvb, offset)) {
575     length         = tvb_get_ntohs(parameter_list_tvb, offset + PARAMETER_LENGTH_OFFSET);
576     padding_length = nr_of_padding_bytes(length);
577     total_length   = length + padding_length;
578     /* create a tvb for the chunk including the padding bytes */
579     parameter_tvb    = tvb_new_subset(parameter_list_tvb, offset, total_length, total_length);
580     dissect_parameter(parameter_tvb, pinfo, tree); 
581     /* get rid of the handled parameter */
582     offset += total_length;
583   }
584 }
585
586 static void
587 dissect_heartbeat_info_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
588 {
589   guint16 length, heartbeat_info_length;
590   
591   length = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
592   
593   heartbeat_info_length = length - HEARTBEAT_INFO_PARAMETER_HEADER_LENGTH;
594
595   proto_tree_add_text(parameter_tree, parameter_tvb, HEARTBEAT_INFO_PARAMETER_INFO_OFFSET, heartbeat_info_length,
596                       "Heartbeat info (%u byte%s)",
597                       heartbeat_info_length, plurality(heartbeat_info_length, "", "s"));
598
599   proto_item_set_text(parameter_item, "Heartbeat info parameter with %u byte%s of info",
600                       heartbeat_info_length, plurality(heartbeat_info_length, "", "s"));
601 }
602
603 static void
604 dissect_ipv4_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
605 {
606   guint32 ipv4_address;
607
608   tvb_memcpy(parameter_tvb, (guint8 *)&ipv4_address, PARAMETER_VALUE_OFFSET, IPV4_ADDRESS_LENGTH); 
609   proto_tree_add_ipv4(parameter_tree, hf_sctp_parameter_ipv4_address,
610                       parameter_tvb, PARAMETER_VALUE_OFFSET, IPV4_ADDRESS_LENGTH,
611                       ipv4_address);  
612   proto_item_set_text(parameter_item, "IPV4 address parameter");
613 }
614
615 static void
616 dissect_ipv6_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
617 {
618   proto_tree_add_ipv6(parameter_tree, hf_sctp_parameter_ipv6_address,
619                       parameter_tvb, PARAMETER_VALUE_OFFSET, IPV6_ADDRESS_LENGTH,
620                       tvb_get_ptr(parameter_tvb, PARAMETER_VALUE_OFFSET, IPV6_ADDRESS_LENGTH));
621   
622   proto_item_set_text(parameter_item, "IPV6 address parameter");
623 }
624
625 static void
626 dissect_state_cookie_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
627 {
628   guint16 length, state_cookie_length;
629   
630   length = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
631   
632   state_cookie_length = length - STATE_COOKIE_PARAMETER_HEADER_LENGTH;
633
634   proto_tree_add_text(parameter_tree, parameter_tvb, STATE_COOKIE_PARAMETER_COOKIE_OFFSET, state_cookie_length,
635                       "State cookie (%u byte%s)",
636                       state_cookie_length, plurality(state_cookie_length, "", "s"));
637
638   proto_item_set_text(parameter_item, "State Cookie Parameter with %u byte%s cookie",
639                       state_cookie_length, plurality(state_cookie_length, "", "s"));
640 }
641
642 static void
643 dissect_unrecognized_parameters_parameter(tvbuff_t *parameter_tvb, packet_info *pinfo, proto_tree *parameter_tree, proto_item *parameter_item)
644 {
645   guint16 length, padding_length, parameter_value_length;
646   tvbuff_t *unrecognized_parameters_tvb;
647
648   length = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
649   padding_length = nr_of_padding_bytes(length);
650
651   parameter_value_length = length - PARAMETER_HEADER_LENGTH + padding_length;
652
653   unrecognized_parameters_tvb = tvb_new_subset(parameter_tvb, PARAMETER_VALUE_OFFSET, 
654                                                parameter_value_length, parameter_value_length);
655   dissect_tlv_parameter_list(unrecognized_parameters_tvb, pinfo, parameter_tree);
656    
657   proto_item_set_text(parameter_item, "Unrecognized parameter of type");
658 }
659
660 static void
661 dissect_cookie_preservative_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
662 {
663   guint32 increment;
664
665   increment =  tvb_get_ntohl(parameter_tvb, COOKIE_PRESERVATIVE_PARAMETER_INCR_OFFSET);
666   
667   proto_tree_add_uint(parameter_tree, hf_sctp_parameter_cookie_preservative_increment, parameter_tvb, 
668                       COOKIE_PRESERVATIVE_PARAMETER_INCR_OFFSET, 
669                       COOKIE_PRESERVATIVE_PARAMETER_INCR_LENGTH,
670                       increment);
671   
672   proto_item_set_text(parameter_item, "Cookie preservative parameter requesting for a %u msec increment",
673                       increment);
674 }
675
676 static void
677 dissect_hostname_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
678 {
679   guint16  length, hostname_length;
680   char *hostname;
681
682   length = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
683   
684   hostname_length = length - PARAMETER_HEADER_LENGTH;
685   hostname = (char *)tvb_get_ptr(parameter_tvb, PARAMETER_VALUE_OFFSET, hostname_length);
686   proto_tree_add_string(parameter_tree, hf_sctp_parameter_hostname_hostname, parameter_tvb,
687                         PARAMETER_VALUE_OFFSET, hostname_length,
688                         hostname);
689
690   proto_item_set_text(parameter_item, "Hostname parameter");
691 }
692
693 static void
694 dissect_supported_address_types_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
695 {
696   guint16 length, address_type, number_of_address_types, address_type_number, list_of_address_types_length ;
697   guint offset;
698   proto_item *address_list_item;
699   proto_tree *address_list_tree;
700
701   length = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
702   list_of_address_types_length = length - PARAMETER_HEADER_LENGTH;
703   number_of_address_types = list_of_address_types_length / SUPPORTED_ADDRESS_TYPE_PARAMETER_ADDRESS_TYPE_LENGTH;
704
705   address_list_item = proto_tree_add_text(parameter_tree, parameter_tvb, PARAMETER_VALUE_OFFSET, list_of_address_types_length,
706                                           "Supported Address Types (%u address type%s)",
707                                           number_of_address_types, plurality(number_of_address_types, "", "s"));
708   address_list_tree = proto_item_add_subtree(address_list_item, ett_sctp_supported_address_types_parameter);
709  
710   offset = PARAMETER_VALUE_OFFSET;
711   for(address_type_number = 1; address_type_number <= number_of_address_types; address_type_number++) {
712     address_type = tvb_get_ntohs(parameter_tvb, offset);
713     proto_tree_add_uint_format(address_list_tree, hf_sctp_supported_address_types_parameter,
714                                parameter_tvb, offset, SUPPORTED_ADDRESS_TYPE_PARAMETER_ADDRESS_TYPE_LENGTH,
715                                address_type, "Supported address type: 0x%04x (%s)",
716                                address_type, val_to_str(address_type, sctp_parameter_identifier_values, "unknown"));
717     offset += SUPPORTED_ADDRESS_TYPE_PARAMETER_ADDRESS_TYPE_LENGTH;
718   };
719
720   proto_item_set_text(parameter_item, "Supported address types parameter reporting %u address type%s",
721                       number_of_address_types, plurality(number_of_address_types, "", "s"));
722 }
723
724 static void
725 dissect_ecn_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
726 {
727    proto_item_set_text(parameter_item, "ECN parameter");
728 }
729
730 #define USTREAMS_START_LENGTH    2
731 #define USTREAMS_END_LENGTH      2
732 #define USTREAMS_INTERVAL_LENGTH (USTREAMS_START_LENGTH + USTREAMS_END_LENGTH)
733
734 static void
735 dissect_unreliable_streams_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
736 {  
737     guint16 length, start, end, number_of_intervals, interval_number;
738     proto_item *interval_item;
739     proto_tree *interval_tree;
740     gint interval_offset;
741     
742     length              = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
743     number_of_intervals = (length - PARAMETER_HEADER_LENGTH) / USTREAMS_INTERVAL_LENGTH;
744     
745     interval_offset     = PARAMETER_VALUE_OFFSET;
746     for(interval_number = 1; interval_number <= number_of_intervals; interval_number++) {
747       start = tvb_get_ntohs(parameter_tvb, interval_offset);
748       end   = tvb_get_ntohs(parameter_tvb, interval_offset + USTREAMS_START_LENGTH);
749       interval_item = proto_tree_add_text(parameter_tree, parameter_tvb, interval_offset, USTREAMS_INTERVAL_LENGTH, "Unreliable streams (%u-%u)", start, end);
750       interval_tree = proto_item_add_subtree(interval_item, ett_sctp_unreliable_streams_interval);
751       proto_tree_add_uint(interval_tree, hf_sctp_ustreams_start, parameter_tvb, interval_offset, USTREAMS_START_LENGTH, start);
752       proto_tree_add_uint(interval_tree, hf_sctp_ustreams_end,  parameter_tvb, interval_offset + USTREAMS_START_LENGTH, USTREAMS_END_LENGTH, end);
753       interval_offset += USTREAMS_INTERVAL_LENGTH;
754     };
755    proto_item_set_text(parameter_item, "Unreliable streams parameter");
756 }
757
758 static void
759 dissect_add_ip_address_parameter(tvbuff_t *parameter_tvb, packet_info *pinfo, proto_tree *parameter_tree, proto_item *parameter_item)
760 {
761   guint16 length, parameter_value_length;
762   tvbuff_t *address_tvb;
763
764   length                 = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
765   parameter_value_length = length - PARAMETER_HEADER_LENGTH;
766
767   address_tvb            =  tvb_new_subset(parameter_tvb, PARAMETER_VALUE_OFFSET, parameter_value_length, parameter_value_length);
768   dissect_parameter(address_tvb, pinfo, parameter_tree); 
769
770   proto_item_set_text(parameter_item, "Add IP address parameter");
771 }
772
773 static void
774 dissect_del_ip_address_parameter(tvbuff_t *parameter_tvb, packet_info *pinfo, proto_tree *parameter_tree, proto_item *parameter_item)
775 {
776   guint16 length, parameter_value_length;
777   tvbuff_t *address_tvb;
778
779   length                 = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
780   parameter_value_length = length - PARAMETER_HEADER_LENGTH;
781
782   address_tvb            =  tvb_new_subset(parameter_tvb, PARAMETER_VALUE_OFFSET, parameter_value_length, parameter_value_length);
783   dissect_parameter(address_tvb, pinfo, parameter_tree); 
784
785   proto_item_set_text(parameter_item, "Delete IP address parameter");
786 }
787
788 static void
789 dissect_error_cause_indication_parameter(tvbuff_t *parameter_tvb, packet_info *pinfo, proto_tree *parameter_tree, proto_item *parameter_item)
790 {
791   guint16 length, padding_length, total_length;
792   gint offset;
793   tvbuff_t *error_cause_tvb;
794
795   offset = PARAMETER_VALUE_OFFSET;
796   while(tvb_length_remaining(parameter_tvb, offset)) {
797     length         = tvb_get_ntohs(parameter_tvb, offset + CAUSE_LENGTH_OFFSET);
798     padding_length = nr_of_padding_bytes(length);
799     total_length   = length + padding_length;
800     /* create a tvb for the chunk including the padding bytes */
801     error_cause_tvb    = tvb_new_subset(parameter_tvb, offset , total_length, total_length);
802     dissect_error_cause(error_cause_tvb, pinfo, parameter_tree); 
803     /* get rid of the handled parameter */
804     offset += total_length;
805   }
806   proto_item_set_text(parameter_item, "Error cause indication");
807 }
808
809 static void
810 dissect_set_primary_address_parameter(tvbuff_t *parameter_tvb, packet_info *pinfo, proto_tree *parameter_tree, proto_item *parameter_item)
811 {
812   guint16 length, parameter_value_length;
813   tvbuff_t *address_tvb;
814
815   length                 = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
816   parameter_value_length = length - PARAMETER_HEADER_LENGTH;
817
818   address_tvb            =  tvb_new_subset(parameter_tvb, PARAMETER_VALUE_OFFSET, parameter_value_length, parameter_value_length);
819   dissect_parameter(address_tvb, pinfo, parameter_tree); 
820
821   proto_item_set_text(parameter_item, "Set primary address parameter");
822 }
823
824 static void
825 dissect_success_report_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
826 {
827    proto_item_set_text(parameter_item, "Success report parameter");
828 }
829
830 #define ADAP_INDICATION_LENGTH 4
831 #define ADAP_INDICATION_OFFSET PARAMETER_VALUE_OFFSET
832
833 static void
834 dissect_adap_indication_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
835 {
836   guint32 indication;
837
838   indication =  tvb_get_ntohl(parameter_tvb, ADAP_INDICATION_OFFSET);
839   proto_tree_add_uint(parameter_tree, hf_sctp_adap_indication, parameter_tvb, ADAP_INDICATION_OFFSET, ADAP_INDICATION_LENGTH, indication);
840   proto_item_set_text(parameter_item, "Adaptation layer indication");
841 }
842
843 static void
844 dissect_unknown_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
845 {
846   guint16 type, length, parameter_value_length;
847   
848   type   = tvb_get_ntohs(parameter_tvb, PARAMETER_TYPE_OFFSET);
849   length = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
850   
851   parameter_value_length = length - PARAMETER_HEADER_LENGTH;
852
853   proto_tree_add_text(parameter_tree, parameter_tvb, PARAMETER_VALUE_OFFSET, parameter_value_length,
854                       "Parameter value (%u byte%s)",
855                       parameter_value_length, plurality(parameter_value_length, "", "s"));
856
857   proto_item_set_text(parameter_item, "Parameter of type %u and %u byte%s value",
858                       type, parameter_value_length, plurality(parameter_value_length, "", "s"));
859 }
860
861 static void
862 dissect_parameter(tvbuff_t *parameter_tvb, packet_info *pinfo, proto_tree *chunk_tree)
863 {
864   guint16 type, length, padding_length, total_length;
865   proto_item *parameter_item;
866   proto_tree *parameter_tree;
867
868   type           = tvb_get_ntohs(parameter_tvb, PARAMETER_TYPE_OFFSET);
869   length         = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
870   padding_length = nr_of_padding_bytes(length);
871   total_length   = length + padding_length;
872    
873   parameter_item = proto_tree_add_text(chunk_tree, parameter_tvb,
874         PARAMETER_HEADER_OFFSET, total_length, "%s parameter",
875         val_to_str(type, sctp_parameter_identifier_values, "Unknown"));
876   parameter_tree = proto_item_add_subtree(parameter_item, ett_sctp_chunk_parameter);
877  
878   proto_tree_add_uint(parameter_tree, hf_sctp_chunk_parameter_type, 
879                       parameter_tvb, PARAMETER_TYPE_OFFSET, PARAMETER_TYPE_LENGTH,
880                       type);
881   proto_tree_add_uint(parameter_tree, hf_sctp_chunk_parameter_length, 
882                       parameter_tvb, PARAMETER_LENGTH_OFFSET, PARAMETER_LENGTH_LENGTH,
883                       length);
884  
885   switch(type) {
886   case HEARTBEAT_INFO_PARAMETER_ID:
887     dissect_heartbeat_info_parameter(parameter_tvb, parameter_tree, parameter_item);
888     break;
889   case IPV4ADDRESS_PARAMETER_ID:
890     dissect_ipv4_parameter(parameter_tvb, parameter_tree, parameter_item);
891     break;
892   case IPV6ADDRESS_PARAMETER_ID:
893     dissect_ipv6_parameter(parameter_tvb, parameter_tree, parameter_item);
894     break;
895   case STATE_COOKIE_PARAMETER_ID:
896     dissect_state_cookie_parameter(parameter_tvb, parameter_tree, parameter_item);
897     break;
898   case UNREC_PARA_PARAMETER_ID:
899     dissect_unrecognized_parameters_parameter(parameter_tvb, pinfo,  parameter_tree, parameter_item);
900     break;
901   case COOKIE_PRESERVATIVE_PARAMETER_ID:
902     dissect_cookie_preservative_parameter(parameter_tvb, parameter_tree, parameter_item);
903     break;
904   case HOSTNAME_ADDRESS_PARAMETER_ID:
905     dissect_hostname_parameter(parameter_tvb, parameter_tree, parameter_item);
906     break;
907   case SUPPORTED_ADDRESS_TYPES_PARAMETER_ID:
908     dissect_supported_address_types_parameter(parameter_tvb, parameter_tree, parameter_item);
909     break;
910   case ECN_PARAMETER_ID:
911     dissect_ecn_parameter(parameter_tvb, parameter_tree, parameter_item);
912     break;
913   case UNRELIABLE_STREAMS_PARAMETER_ID:
914     dissect_unreliable_streams_parameter(parameter_tvb, parameter_tree, parameter_item);
915     break;
916   case ADD_IP_ADDRESS_PARAMETER_ID:
917     dissect_add_ip_address_parameter(parameter_tvb, pinfo, parameter_tree, parameter_item);
918     break;
919   case DEL_IP_ADDRESS_PARAMETER_ID:
920     dissect_del_ip_address_parameter(parameter_tvb, pinfo, parameter_tree, parameter_item);
921     break;
922   case ERROR_CAUSE_INDICATION_PARAMETER_ID:
923     dissect_error_cause_indication_parameter(parameter_tvb, pinfo, parameter_tree, parameter_item);
924     break;
925   case SET_PRIMARY_ADDRESS_PARAMETER_ID:
926     dissect_set_primary_address_parameter(parameter_tvb, pinfo, parameter_tree, parameter_item);
927     break;    
928   case SUCCESS_REPORT_PARAMETER_ID:
929     dissect_success_report_parameter(parameter_tvb, parameter_tree, parameter_item);
930     break; 
931   case ADAP_LAYER_INDICATION_PARAMETER_ID:
932     dissect_adap_indication_parameter(parameter_tvb, parameter_tree, parameter_item);
933     break;    
934   default:
935     dissect_unknown_parameter(parameter_tvb, parameter_tree, parameter_item);
936     break;
937   };
938   if ((padding_length > 0) && (type != UNREC_PARA_PARAMETER_ID))
939     proto_tree_add_text(parameter_tree, parameter_tvb, PARAMETER_HEADER_OFFSET + length, padding_length,
940                         "Padding: %u byte%s",
941                         padding_length, plurality(padding_length, "", "s"));
942 }
943
944 /*
945  * Code to handle error causes for ABORT and ERROR chunks
946  */
947 static void
948 dissect_invalid_stream_identifier_cause(tvbuff_t *cause_tvb, proto_tree *cause_tree, proto_item *cause_item)
949 {
950   guint16 stream_identifier;
951
952   stream_identifier   = tvb_get_ntohs(cause_tvb, CAUSE_STREAM_IDENTIFIER_OFFSET);
953   proto_tree_add_uint(cause_tree, hf_sctp_cause_stream_identifier, 
954                       cause_tvb, CAUSE_STREAM_IDENTIFIER_OFFSET, CAUSE_STREAM_IDENTIFIER_LENGTH,
955                       stream_identifier);
956   proto_tree_add_text(cause_tree, cause_tvb, CAUSE_RESERVED_OFFSET, CAUSE_RESERVED_LENGTH,
957                       "Reserved (2 bytes)");
958
959   proto_item_set_text(cause_item, "Error cause reporting invalid stream identifier %u",
960                       stream_identifier);
961 }
962
963 static void
964 dissect_missing_mandatory_parameters_cause(tvbuff_t *cause_tvb, proto_tree *cause_tree, proto_item *cause_item)
965 {
966   guint32 number_of_missing_parameters, missing_parameter_number;
967   guint16 parameter_type;
968   guint   offset;
969
970   number_of_missing_parameters = tvb_get_ntohl(cause_tvb, CAUSE_NUMBER_OF_MISSING_PARAMETERS_OFFSET);
971   proto_tree_add_uint(cause_tree, hf_sctp_cause_number_of_missing_parameters, 
972                       cause_tvb, CAUSE_NUMBER_OF_MISSING_PARAMETERS_OFFSET, CAUSE_NUMBER_OF_MISSING_PARAMETERS_LENGTH,
973                       number_of_missing_parameters);
974   offset = CAUSE_FIRST_MISSING_PARAMETER_TYPE_OFFSET;
975   for(missing_parameter_number = 1; missing_parameter_number <= number_of_missing_parameters; missing_parameter_number++) {
976     parameter_type = tvb_get_ntohs(cause_tvb, offset);
977     proto_tree_add_uint(cause_tree, hf_sctp_cause_missing_parameter_type,
978                         cause_tvb, offset, CAUSE_MISSING_PARAMETER_TYPE_LENGTH,
979                         parameter_type);
980     offset +=  CAUSE_MISSING_PARAMETER_TYPE_LENGTH;
981   };
982
983   proto_item_set_text(cause_item, "Error cause reporting %u missing mandatory parameter%s",
984                       number_of_missing_parameters, plurality(number_of_missing_parameters, "", "s") );
985 }
986
987 static void
988 dissect_stale_cookie_error_cause(tvbuff_t *cause_tvb, proto_tree *cause_tree, proto_item *cause_item)
989 {
990   guint32 measure_of_staleness;
991
992   measure_of_staleness =  tvb_get_ntohl(cause_tvb, CAUSE_MEASURE_OF_STALENESS_OFFSET);
993   
994   proto_tree_add_uint(cause_tree, hf_sctp_cause_measure_of_staleness, cause_tvb, 
995                       CAUSE_MEASURE_OF_STALENESS_OFFSET, 
996                       CAUSE_MEASURE_OF_STALENESS_LENGTH,
997                       measure_of_staleness);
998   
999   proto_item_set_text(cause_item, "Error cause reporting a measure of staleness of %u usec",
1000                       measure_of_staleness);
1001 }
1002
1003 static void
1004 dissect_out_of_resource_cause(tvbuff_t *cause_tvb, proto_tree *cause_tree, proto_item *cause_item)
1005 {
1006   proto_item_set_text(cause_item, "Error cause reporting lack of resources");
1007 }
1008
1009 static void
1010 dissect_unresolvable_address_cause(tvbuff_t *cause_tvb, packet_info *pinfo, proto_tree *cause_tree, proto_item *cause_item)
1011 {
1012   guint16 code, length, parameter_length, parameter_type;
1013   tvbuff_t *parameter_tvb;
1014
1015   code   = tvb_get_ntohs(cause_tvb, CAUSE_CODE_OFFSET);
1016   length = tvb_get_ntohs(cause_tvb, CAUSE_LENGTH_OFFSET);
1017   
1018   parameter_length = length - CAUSE_HEADER_LENGTH;
1019   parameter_tvb = tvb_new_subset(cause_tvb, CAUSE_INFO_OFFSET, 
1020                                  parameter_length, parameter_length);
1021
1022   dissect_parameter(parameter_tvb, pinfo, cause_tree);
1023   parameter_type = tvb_get_ntohs(parameter_tvb, PARAMETER_TYPE_OFFSET);
1024  
1025   proto_item_set_text(cause_item, "Error cause reporting unresolvable address of type 0x%04x (%s)",
1026                       parameter_type, val_to_str(parameter_type, sctp_parameter_identifier_values, "unknown") );
1027 }
1028
1029 static void
1030 dissect_unrecognized_chunk_type_cause(tvbuff_t *cause_tvb,  packet_info *pinfo, 
1031                                       proto_tree *cause_tree, proto_item *cause_item)
1032 {
1033   guint16 length, chunk_length;
1034   guint8 unrecognized_type;
1035   tvbuff_t *unrecognized_chunk_tvb;
1036
1037   length = tvb_get_ntohs(cause_tvb, CAUSE_LENGTH_OFFSET);
1038   
1039   chunk_length = length - CAUSE_HEADER_LENGTH;
1040
1041   unrecognized_chunk_tvb = tvb_new_subset(cause_tvb, CAUSE_INFO_OFFSET, chunk_length, chunk_length);
1042   dissect_sctp_chunk(unrecognized_chunk_tvb, pinfo, cause_tree,cause_tree);
1043
1044   unrecognized_type   = tvb_get_guint8(unrecognized_chunk_tvb, CHUNK_TYPE_OFFSET);
1045  
1046   proto_item_set_text(cause_item, "Error cause reporting unrecognized chunk of type %u (%s)",
1047                       unrecognized_type,
1048                       val_to_str(unrecognized_type, sctp_chunk_type_values, "unknown"));
1049 }
1050
1051 static void
1052 dissect_invalid_mandatory_parameter_cause(tvbuff_t *cause_tvb, proto_tree *cause_tree, proto_item *cause_item)
1053 {
1054   proto_item_set_text(cause_item, "Error cause reporting an invalid mandatory parameter");
1055 }
1056
1057 static void
1058 dissect_unrecognized_parameters_cause(tvbuff_t *cause_tvb, packet_info *pinfo, proto_tree *cause_tree, proto_item *cause_item)
1059 {
1060   guint16 length, padding_length, cause_info_length;
1061   tvbuff_t *unrecognized_parameters_tvb;
1062
1063   length            = tvb_get_ntohs(cause_tvb, CAUSE_LENGTH_OFFSET);
1064   padding_length    = nr_of_padding_bytes(length);
1065   cause_info_length = length - CAUSE_HEADER_LENGTH + padding_length;
1066
1067   unrecognized_parameters_tvb = tvb_new_subset(cause_tvb, CAUSE_INFO_OFFSET, cause_info_length, cause_info_length);
1068   dissect_tlv_parameter_list(unrecognized_parameters_tvb, pinfo, cause_tree);
1069  
1070   proto_item_set_text(cause_item, "Error cause reporting unrecognized parameters");
1071 }
1072
1073 static void
1074 dissect_no_user_data_cause(tvbuff_t *cause_tvb, proto_tree *cause_tree, proto_item *cause_item)
1075 {
1076   guint32 tsn;
1077
1078   tsn = tvb_get_ntohl(cause_tvb, CAUSE_TSN_OFFSET);
1079   proto_tree_add_uint(cause_tree, hf_sctp_cause_tsn, cause_tvb, 
1080                       CAUSE_TSN_OFFSET, 
1081                       CAUSE_TSN_LENGTH,
1082                       tsn);
1083
1084   proto_item_set_text(cause_item, "Error cause reporting data chunk with TSN %u contains no data",
1085                       tsn);
1086 }
1087
1088 static void
1089 dissect_cookie_received_while_shutting_down_cause(tvbuff_t *cause_tvb, proto_tree *cause_tree, proto_item *cause_item)
1090 {
1091   proto_item_set_text(cause_item, "Error cause reporting cookie reception while shutting down");
1092 }
1093
1094 static void
1095 dissect_delete_last_address_cause(tvbuff_t *cause_tvb, packet_info *pinfo, proto_tree *cause_tree, proto_item *cause_item)
1096 {
1097   guint16 length, cause_info_length;
1098   tvbuff_t *parameter_tvb;
1099
1100   length = tvb_get_ntohs(cause_tvb, CAUSE_LENGTH_OFFSET);
1101   cause_info_length = length - CAUSE_HEADER_LENGTH;
1102   parameter_tvb    = tvb_new_subset(cause_tvb, CAUSE_INFO_OFFSET, cause_info_length, cause_info_length);
1103   dissect_parameter(parameter_tvb, pinfo, cause_tree); 
1104   proto_item_set_text(cause_item, "Delete last address cause");
1105 }
1106
1107 static void
1108 dissect_resource_outage_cause(tvbuff_t *cause_tvb, packet_info *pinfo, proto_tree *cause_tree, proto_item *cause_item)
1109 {
1110   guint16 length, cause_info_length;
1111   tvbuff_t *parameter_tvb;
1112
1113   length = tvb_get_ntohs(cause_tvb, CAUSE_LENGTH_OFFSET);
1114   cause_info_length = length - CAUSE_HEADER_LENGTH;
1115   parameter_tvb    = tvb_new_subset(cause_tvb, CAUSE_INFO_OFFSET, cause_info_length, cause_info_length);
1116   dissect_parameter(parameter_tvb, pinfo, cause_tree); 
1117   proto_item_set_text(cause_item, "Operation refused due to resource shortage");
1118 }
1119
1120 static void
1121 dissect_delete_source_address_cause(tvbuff_t *cause_tvb, packet_info *pinfo, proto_tree *cause_tree, proto_item *cause_item)
1122 {
1123   guint16 length, cause_info_length;
1124   tvbuff_t *parameter_tvb;
1125
1126   length = tvb_get_ntohs(cause_tvb, CAUSE_LENGTH_OFFSET);
1127   cause_info_length = length - CAUSE_HEADER_LENGTH;
1128   parameter_tvb    = tvb_new_subset(cause_tvb, CAUSE_INFO_OFFSET, cause_info_length, cause_info_length);
1129   dissect_parameter(parameter_tvb, pinfo, cause_tree); 
1130   proto_item_set_text(cause_item, "Delete source address cause");
1131 }
1132
1133 static void
1134 dissect_unknown_cause(tvbuff_t *cause_tvb, proto_tree *cause_tree, proto_item *cause_item)
1135 {
1136   guint16 code, length, cause_info_length;
1137
1138   code   = tvb_get_ntohs(cause_tvb, CAUSE_CODE_OFFSET);
1139   length = tvb_get_ntohs(cause_tvb, CAUSE_LENGTH_OFFSET);
1140   
1141   cause_info_length = length - CAUSE_HEADER_LENGTH;
1142
1143   proto_tree_add_text(cause_tree, cause_tvb, CAUSE_INFO_OFFSET, cause_info_length,
1144                       "Cause specific information (%u byte%s)",
1145                       cause_info_length, plurality(cause_info_length, "", "s"));
1146
1147   proto_item_set_text(cause_item, "Error cause with code %u and %u byte%s information",
1148                       code, cause_info_length, plurality(cause_info_length, "", "s"));
1149 }
1150
1151 static void
1152 dissect_error_cause(tvbuff_t *cause_tvb, packet_info *pinfo, proto_tree *chunk_tree)
1153 {
1154   guint16 code, length, padding_length, total_length;
1155   proto_item *cause_item;
1156   proto_tree *cause_tree;
1157
1158   code           = tvb_get_ntohs(cause_tvb, CAUSE_CODE_OFFSET);
1159   length         = tvb_get_ntohs(cause_tvb, CAUSE_LENGTH_OFFSET);
1160   padding_length = nr_of_padding_bytes(length);
1161   total_length   = length + padding_length;
1162
1163   cause_item = proto_tree_add_text(chunk_tree, cause_tvb,
1164                                    CAUSE_HEADER_OFFSET, total_length,
1165                                    "BAD ERROR CAUSE");
1166   cause_tree = proto_item_add_subtree(cause_item, ett_sctp_chunk_cause);
1167  
1168   proto_tree_add_uint(cause_tree, hf_sctp_cause_code, 
1169                       cause_tvb, CAUSE_CODE_OFFSET, CAUSE_CODE_LENGTH,
1170                       code);
1171   proto_tree_add_uint(cause_tree, hf_sctp_cause_length, 
1172                       cause_tvb, CAUSE_LENGTH_OFFSET, CAUSE_LENGTH_LENGTH,
1173                       length);
1174  
1175   switch(code) {
1176   case INVALID_STREAM_IDENTIFIER:
1177     dissect_invalid_stream_identifier_cause(cause_tvb, cause_tree, cause_item);
1178     break;
1179   case MISSING_MANDATORY_PARAMETERS:
1180     dissect_missing_mandatory_parameters_cause(cause_tvb, cause_tree, cause_item);
1181     break;
1182   case STALE_COOKIE_ERROR:
1183     dissect_stale_cookie_error_cause(cause_tvb, cause_tree, cause_item);
1184     break;
1185   case OUT_OF_RESOURCE:
1186     dissect_out_of_resource_cause(cause_tvb, cause_tree, cause_item);
1187     break;
1188   case UNRESOLVABLE_ADDRESS:
1189     dissect_unresolvable_address_cause(cause_tvb, pinfo, cause_tree, cause_item);
1190     break;
1191   case UNRECOGNIZED_CHUNK_TYPE:
1192     dissect_unrecognized_chunk_type_cause(cause_tvb, pinfo, cause_tree, cause_item);
1193     break;
1194   case INVALID_MANDATORY_PARAMETER:
1195     dissect_invalid_mandatory_parameter_cause(cause_tvb, cause_tree, cause_item);
1196     break;
1197   case UNRECOGNIZED_PARAMETERS:
1198     dissect_unrecognized_parameters_cause(cause_tvb, pinfo, cause_tree, cause_item);
1199     break;
1200   case NO_USER_DATA:
1201     dissect_no_user_data_cause(cause_tvb, cause_tree, cause_item);
1202     break;
1203   case COOKIE_RECEIVED_WHILE_SHUTTING_DOWN:
1204     dissect_cookie_received_while_shutting_down_cause(cause_tvb, cause_tree, cause_item);
1205     break;
1206   case REQUEST_TO_DELETE_LAST_ADDRESS:
1207     dissect_delete_last_address_cause(cause_tvb, pinfo, cause_tree, cause_item);
1208     break;
1209   case OPERATION_REFUSED_DUE_TO_RESOURCE_SHORTAGE:
1210     dissect_resource_outage_cause(cause_tvb, pinfo, cause_tree, cause_item);
1211     break;
1212   case REQUEST_TO_DELETE_SOURCE_ADDRESS:
1213     dissect_delete_source_address_cause(cause_tvb, pinfo, cause_tree, cause_item);
1214     break;
1215   default:
1216     dissect_unknown_cause(cause_tvb, cause_tree, cause_item);
1217     break;
1218   };
1219   if ((padding_length > 0) && (code != UNRECOGNIZED_PARAMETERS))
1220     proto_tree_add_text(cause_tree, cause_tvb, CAUSE_HEADER_OFFSET + length, padding_length,
1221                         "Padding: %u byte%s",
1222                         padding_length, plurality(padding_length, "", "s"));
1223 }
1224
1225 /*
1226  * Code to actually dissect the packets 
1227 */
1228
1229 static gboolean
1230 dissect_payload(tvbuff_t *payload_tvb, packet_info *pinfo, proto_tree *tree,
1231                 proto_tree *chunk_tree, guint32 ppi, guint16 payload_length, guint16 padding_length)
1232 {
1233   /* do lookup with the subdissector table */
1234   if (dissector_try_port (sctp_ppi_dissector_table, ppi,  payload_tvb, pinfo, tree) ||
1235       dissector_try_port(sctp_port_dissector_table, pinfo->srcport,  payload_tvb, pinfo, tree) ||
1236       dissector_try_port(sctp_port_dissector_table, pinfo->destport, payload_tvb, pinfo, tree)){
1237     return TRUE;
1238   }
1239   else {
1240     if (check_col(pinfo->cinfo, COL_INFO))
1241       col_append_str(pinfo->cinfo, COL_INFO, "DATA ");
1242     proto_tree_add_text(chunk_tree, payload_tvb, 0, payload_length,
1243                         "Payload (%u byte%s)",
1244                         payload_length, plurality(payload_length, "", "s")); 
1245     if (padding_length > 0)
1246       proto_tree_add_text(chunk_tree, payload_tvb, payload_length, padding_length,
1247                           "Padding: %u byte%s",
1248                           padding_length, plurality(padding_length, "", "s"));
1249     return FALSE;
1250   }
1251 }
1252
1253 static gboolean
1254 dissect_data_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
1255                    proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
1256
1257   guint8  flags;
1258   guint16 length, total_payload_length, payload_length, padding_length, stream_id, stream_seq_number;
1259   guint32 tsn, payload_proto_id;
1260   proto_tree *flag_tree;
1261   tvbuff_t *payload_tvb;
1262    
1263   length            = tvb_get_ntohs(chunk_tvb, CHUNK_LENGTH_OFFSET);
1264   payload_length       = length - DATA_CHUNK_HEADER_LENGTH;
1265   padding_length       = nr_of_padding_bytes(length);
1266   total_payload_length = payload_length + padding_length;
1267   payload_tvb          = tvb_new_subset(chunk_tvb, DATA_CHUNK_PAYLOAD_OFFSET,
1268                                           total_payload_length, total_payload_length);
1269   payload_proto_id     = tvb_get_ntohl(chunk_tvb, DATA_CHUNK_PAYLOAD_PROTOCOL_ID_OFFSET);
1270
1271   if (chunk_tree) {
1272     flags             = tvb_get_guint8(chunk_tvb, CHUNK_FLAGS_OFFSET);
1273      
1274     flag_tree = proto_item_add_subtree(flags_item, ett_sctp_data_chunk_flags);
1275     proto_tree_add_boolean(flag_tree, hf_sctp_data_chunk_e_bit, chunk_tvb,
1276                            CHUNK_FLAGS_OFFSET, CHUNK_FLAGS_LENGTH, flags);
1277     proto_tree_add_boolean(flag_tree, hf_sctp_data_chunk_b_bit, chunk_tvb,
1278                            CHUNK_FLAGS_OFFSET, CHUNK_FLAGS_LENGTH, flags);
1279     proto_tree_add_boolean(flag_tree, hf_sctp_data_chunk_u_bit, chunk_tvb,
1280                            CHUNK_FLAGS_OFFSET, CHUNK_FLAGS_LENGTH, flags);
1281     
1282     tsn               = tvb_get_ntohl(chunk_tvb, DATA_CHUNK_TSN_OFFSET);
1283     stream_id         = tvb_get_ntohs(chunk_tvb, DATA_CHUNK_STREAM_ID_OFFSET);
1284     stream_seq_number = tvb_get_ntohs(chunk_tvb, DATA_CHUNK_STREAM_SEQ_NUMBER_OFFSET);
1285          
1286     proto_tree_add_uint(chunk_tree, hf_sctp_data_chunk_tsn, 
1287                         chunk_tvb,
1288                         DATA_CHUNK_TSN_OFFSET, DATA_CHUNK_TSN_LENGTH,
1289                         tsn);
1290     proto_tree_add_uint(chunk_tree, hf_sctp_data_chunk_stream_id, 
1291                         chunk_tvb, 
1292                         DATA_CHUNK_STREAM_ID_OFFSET, DATA_CHUNK_STREAM_ID_LENGTH,
1293                         stream_id);
1294     proto_tree_add_uint(chunk_tree, hf_sctp_data_chunk_stream_seq_number, 
1295                         chunk_tvb, 
1296                         DATA_CHUNK_STREAM_SEQ_NUMBER_OFFSET, DATA_CHUNK_STREAM_SEQ_NUMBER_LENGTH,
1297                         stream_seq_number);
1298     proto_tree_add_uint(chunk_tree, hf_sctp_data_chunk_payload_proto_id, 
1299                         chunk_tvb, DATA_CHUNK_PAYLOAD_PROTOCOL_ID_OFFSET, DATA_CHUNK_PAYLOAD_PROTOCOL_ID_LENGTH,
1300                         payload_proto_id);
1301     proto_item_set_text(chunk_item, "DATA chunk with TSN %u (%u:%u) containing %u byte%s of payload",
1302                         tsn, stream_id, stream_seq_number, 
1303                         payload_length, plurality(payload_length, "", "s"));
1304   };   
1305   return dissect_payload(payload_tvb, pinfo, tree, chunk_tree, payload_proto_id, payload_length, padding_length); 
1306 }
1307
1308 static void
1309 dissect_init_chunk(tvbuff_t *chunk_tvb,  packet_info *pinfo, proto_tree *tree,
1310                    proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
1311
1312   guint32 initiate_tag, adv_rec_window_credit, initial_tsn;
1313   guint16 number_of_inbound_streams, number_of_outbound_streams;
1314   guint8  type;
1315   tvbuff_t *parameter_list_tvb;
1316
1317   type                       = tvb_get_guint8(chunk_tvb, CHUNK_TYPE_OFFSET);
1318   
1319   if (check_col(pinfo->cinfo, COL_INFO)) {
1320     if (type == SCTP_INIT_CHUNK_ID) {
1321       col_append_str(pinfo->cinfo, COL_INFO, "INIT ");
1322     } else {
1323       col_append_str(pinfo->cinfo, COL_INFO, "INIT_ACK ");
1324     };
1325   };
1326   
1327   if (chunk_tree) {
1328     initiate_tag               = tvb_get_ntohl(chunk_tvb, INIT_CHUNK_INITIATE_TAG_OFFSET);
1329     adv_rec_window_credit      = tvb_get_ntohl(chunk_tvb, INIT_CHUNK_ADV_REC_WINDOW_CREDIT_OFFSET);
1330     number_of_inbound_streams  = tvb_get_ntohs(chunk_tvb, INIT_CHUNK_NUMBER_OF_INBOUND_STREAMS_OFFSET);
1331     number_of_outbound_streams = tvb_get_ntohs(chunk_tvb, INIT_CHUNK_NUMBER_OF_OUTBOUND_STREAMS_OFFSET);
1332     initial_tsn                = tvb_get_ntohl(chunk_tvb, INIT_CHUNK_INITIAL_TSN_OFFSET);
1333     
1334     /* handle fixed parameters */
1335     proto_tree_add_uint(chunk_tree, hf_sctp_init_chunk_initiate_tag, 
1336                         chunk_tvb,
1337                         INIT_CHUNK_INITIATE_TAG_OFFSET, INIT_CHUNK_INITIATE_TAG_LENGTH,
1338                         initiate_tag);
1339     proto_tree_add_uint(chunk_tree, hf_sctp_init_chunk_adv_rec_window_credit, 
1340                         chunk_tvb,
1341                         INIT_CHUNK_ADV_REC_WINDOW_CREDIT_OFFSET, INIT_CHUNK_ADV_REC_WINDOW_CREDIT_LENGTH,
1342                         adv_rec_window_credit);
1343     proto_tree_add_uint(chunk_tree, hf_sctp_init_chunk_number_of_outbound_streams, 
1344                         chunk_tvb,
1345                         INIT_CHUNK_NUMBER_OF_OUTBOUND_STREAMS_OFFSET, INIT_CHUNK_NUMBER_OF_OUTBOUND_STREAMS_LENGTH,
1346                         number_of_outbound_streams);
1347     proto_tree_add_uint(chunk_tree, hf_sctp_init_chunk_number_of_inbound_streams, 
1348                         chunk_tvb,
1349                         INIT_CHUNK_NUMBER_OF_INBOUND_STREAMS_OFFSET, INIT_CHUNK_NUMBER_OF_INBOUND_STREAMS_LENGTH,
1350                         number_of_inbound_streams);
1351     proto_tree_add_uint(chunk_tree, hf_sctp_init_chunk_initial_tsn, 
1352                         chunk_tvb,
1353                         INIT_CHUNK_INITIAL_TSN_OFFSET, INIT_CHUNK_INITIAL_TSN_LENGTH,
1354                         initial_tsn);
1355     
1356     /* handle variable paramters */
1357     parameter_list_tvb = tvb_new_subset(chunk_tvb, INIT_CHUNK_VARIABLE_LENGTH_PARAMETER_OFFSET, -1, -1);
1358     dissect_tlv_parameter_list(parameter_list_tvb, pinfo, chunk_tree);
1359     
1360     proto_item_set_text(chunk_item, 
1361                         "%s chunk requesting for %u outbound stream%s and accepting up to %u inbound stream%s",
1362                         val_to_str(type, sctp_chunk_type_values, "unknown"),
1363                         number_of_outbound_streams, plurality(number_of_outbound_streams, "", "s"),
1364                         number_of_inbound_streams, plurality(number_of_inbound_streams, "", "s"));
1365   }
1366
1367
1368 static void
1369 dissect_init_ack_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
1370                        proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
1371
1372   dissect_init_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
1373
1374
1375 static void
1376 dissect_sack_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
1377                    proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
1378
1379   guint32 cumulative_tsn_ack, adv_rec_window_credit, dup_tsn;
1380   guint16 number_of_gap_blocks, number_of_dup_tsns;
1381   guint16 gap_block_number, dup_tsn_number, start, end;
1382   gint gap_block_offset, dup_tsn_offset;
1383   proto_item *block_item;
1384   proto_tree *block_tree;
1385   
1386   if (check_col(pinfo->cinfo, COL_INFO))
1387     col_append_str(pinfo->cinfo, COL_INFO, "SACK ");
1388
1389   if (chunk_tree) {
1390     cumulative_tsn_ack    = tvb_get_ntohl(chunk_tvb, SACK_CHUNK_CUMULATIVE_TSN_ACK_OFFSET);
1391     adv_rec_window_credit = tvb_get_ntohl(chunk_tvb, SACK_CHUNK_ADV_REC_WINDOW_CREDIT_OFFSET);
1392     number_of_gap_blocks  = tvb_get_ntohs(chunk_tvb, SACK_CHUNK_NUMBER_OF_GAP_BLOCKS_OFFSET);
1393     number_of_dup_tsns    = tvb_get_ntohs(chunk_tvb, SACK_CHUNK_NUMBER_OF_DUP_TSNS_OFFSET);
1394     
1395     proto_tree_add_uint(chunk_tree, hf_sctp_sack_chunk_cumulative_tsn_ack, 
1396                         chunk_tvb,
1397                         SACK_CHUNK_CUMULATIVE_TSN_ACK_OFFSET, SACK_CHUNK_CUMULATIVE_TSN_ACK_LENGTH,
1398                         cumulative_tsn_ack);
1399     proto_tree_add_uint(chunk_tree, hf_sctp_sack_chunk_adv_rec_window_credit, 
1400                         chunk_tvb,
1401                         SACK_CHUNK_ADV_REC_WINDOW_CREDIT_OFFSET, SACK_CHUNK_ADV_REC_WINDOW_CREDIT_LENGTH,
1402                         adv_rec_window_credit);
1403     proto_tree_add_uint(chunk_tree, hf_sctp_sack_chunk_number_of_gap_blocks, 
1404                         chunk_tvb,
1405                         SACK_CHUNK_NUMBER_OF_GAP_BLOCKS_OFFSET, SACK_CHUNK_NUMBER_OF_GAP_BLOCKS_LENGTH,
1406                         number_of_gap_blocks);
1407     proto_tree_add_uint(chunk_tree, hf_sctp_sack_chunk_number_of_dup_tsns, 
1408                         chunk_tvb,
1409                         SACK_CHUNK_NUMBER_OF_DUP_TSNS_OFFSET, SACK_CHUNK_NUMBER_OF_DUP_TSNS_LENGTH,
1410                         number_of_dup_tsns);
1411     
1412     /* handle the gap acknowledgement blocks */
1413     gap_block_offset = SACK_CHUNK_GAP_BLOCK_OFFSET;
1414     for(gap_block_number = 1; gap_block_number <= number_of_gap_blocks; gap_block_number++) {
1415       start = tvb_get_ntohs(chunk_tvb, gap_block_offset);
1416       end   = tvb_get_ntohs(chunk_tvb, gap_block_offset + SACK_CHUNK_GAP_BLOCK_START_LENGTH);
1417       block_item = proto_tree_add_text(chunk_tree, chunk_tvb,
1418                                        gap_block_offset, SACK_CHUNK_GAP_BLOCK_LENGTH,
1419                                        "Gap Acknowledgement for %u TSN%s",
1420                                        1 + end - start, plurality(1 + end - start, "", "s"));
1421       block_tree = proto_item_add_subtree(block_item, ett_sctp_sack_chunk_gap_block);
1422       proto_tree_add_uint(block_tree, hf_sctp_sack_chunk_gap_block_start, 
1423                           chunk_tvb,
1424                           gap_block_offset, SACK_CHUNK_GAP_BLOCK_START_LENGTH,
1425                           start);
1426       proto_tree_add_uint(block_tree, hf_sctp_sack_chunk_gap_block_end, 
1427                           chunk_tvb,
1428                           gap_block_offset + SACK_CHUNK_GAP_BLOCK_START_LENGTH,
1429                           SACK_CHUNK_GAP_BLOCK_END_LENGTH,
1430                           end);
1431       gap_block_offset += SACK_CHUNK_GAP_BLOCK_LENGTH;
1432     };
1433     
1434     /* handle the duplicate TSNs */
1435     dup_tsn_offset = SACK_CHUNK_GAP_BLOCK_OFFSET + number_of_gap_blocks * SACK_CHUNK_GAP_BLOCK_LENGTH;
1436     for(dup_tsn_number = 1; dup_tsn_number <= number_of_dup_tsns; dup_tsn_number++) {
1437       dup_tsn = tvb_get_ntohl(chunk_tvb, dup_tsn_offset);
1438       proto_tree_add_uint(chunk_tree, hf_sctp_sack_chunk_duplicate_tsn, 
1439                           chunk_tvb,
1440                           dup_tsn, SACK_CHUNK_DUP_TSN_LENGTH,
1441                           dup_tsn);
1442       dup_tsn_offset += SACK_CHUNK_DUP_TSN_LENGTH;
1443     }
1444     
1445     proto_item_set_text(chunk_item, 
1446                         "SACK chunk acknowledging TSN %u and reporting %u gap%s and %u duplicate TSN%s",
1447                         cumulative_tsn_ack,
1448                         number_of_gap_blocks, plurality(number_of_gap_blocks, "", "s"),
1449                         number_of_dup_tsns, plurality(number_of_dup_tsns, "", "s"));
1450   } 
1451 }
1452
1453 static void
1454 dissect_heartbeat_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
1455                         proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
1456 {
1457   tvbuff_t   *parameter_tvb;
1458   guint chunk_length, info_length, padding_length, total_length;
1459     
1460   if (check_col(pinfo->cinfo, COL_INFO))
1461     col_append_str(pinfo->cinfo, COL_INFO, "HEARTBEAT ");
1462
1463   if (chunk_tree) {
1464     chunk_length   = tvb_get_ntohs(chunk_tvb,  CHUNK_LENGTH_OFFSET);
1465     info_length    = chunk_length - CHUNK_HEADER_LENGTH;
1466     padding_length = nr_of_padding_bytes(info_length);
1467     total_length   = info_length + padding_length;
1468     parameter_tvb  = tvb_new_subset(chunk_tvb, HEARTBEAT_CHUNK_INFO_OFFSET, total_length, total_length);
1469     
1470     dissect_parameter(parameter_tvb, pinfo, chunk_tree);
1471     
1472     proto_item_set_text(chunk_item, "HEARTBEAT chunk");
1473   }
1474 }
1475  
1476 static void
1477 dissect_heartbeat_ack_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
1478                             proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
1479 {  
1480   tvbuff_t   *parameter_tvb;
1481   guint chunk_length, info_length, padding_length, total_length;
1482   
1483   if (check_col(pinfo->cinfo, COL_INFO))
1484     col_append_str(pinfo->cinfo, COL_INFO, "HEARTBEAT_ACK ");
1485
1486   if (chunk_tree) {
1487     chunk_length   = tvb_get_ntohs(chunk_tvb,  CHUNK_LENGTH_OFFSET);
1488     info_length    = chunk_length - CHUNK_HEADER_LENGTH;
1489     padding_length = nr_of_padding_bytes(info_length);
1490     total_length   = info_length + padding_length;
1491
1492     parameter_tvb  = tvb_new_subset(chunk_tvb, HEARTBEAT_CHUNK_INFO_OFFSET, total_length, total_length);
1493     
1494     dissect_parameter(parameter_tvb, pinfo, chunk_tree);
1495     
1496     proto_item_set_text(chunk_item, "HEARTBEAT ACK chunk");
1497   } 
1498 }
1499
1500 static void
1501 dissect_abort_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
1502                     proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
1503 {
1504   guint offset, number_of_causes;
1505   guint16 length, padding_length, total_length;
1506   tvbuff_t *cause_tvb;
1507   
1508   if (check_col(pinfo->cinfo, COL_INFO))
1509     col_append_str(pinfo->cinfo, COL_INFO, "ABORT ");
1510
1511   if (chunk_tree) {
1512     number_of_causes = 0;
1513     offset = ABORT_CHUNK_FIRST_ERROR_CAUSE_OFFSET;
1514     while(tvb_length_remaining(chunk_tvb, offset)) {
1515     length         = tvb_get_ntohs(chunk_tvb, offset + CAUSE_LENGTH_OFFSET);
1516     padding_length = nr_of_padding_bytes(length);
1517     total_length   = length + padding_length;
1518     /* create a tvb for the chunk including the padding bytes */
1519     cause_tvb      = tvb_new_subset(chunk_tvb, offset, total_length, total_length);
1520     dissect_error_cause(cause_tvb, pinfo, chunk_tree); 
1521     /* get rid of the handled parameter */
1522     offset += total_length;
1523     number_of_causes++;
1524     };
1525     
1526     proto_item_set_text(chunk_item, "Abort chunk with %u cause%s",
1527                         number_of_causes, plurality(number_of_causes, "", "s"));
1528   } 
1529 }
1530
1531 static void
1532 dissect_shutdown_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
1533                        proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
1534
1535   guint32 cumulative_tsn_ack;
1536  
1537   if (check_col(pinfo->cinfo, COL_INFO))
1538     col_append_str(pinfo->cinfo, COL_INFO, "SHUTDOWN ");
1539
1540   if (chunk_tree) {
1541     cumulative_tsn_ack = tvb_get_ntohl(chunk_tvb, SHUTDOWN_CHUNK_CUMULATIVE_TSN_ACK_OFFSET);
1542     proto_tree_add_uint(chunk_tree, hf_sctp_shutdown_chunk_cumulative_tsn_ack, 
1543                         chunk_tvb,
1544                         SHUTDOWN_CHUNK_CUMULATIVE_TSN_ACK_OFFSET,
1545                         SHUTDOWN_CHUNK_CUMULATIVE_TSN_ACK_LENGTH,
1546                         cumulative_tsn_ack);
1547     
1548     proto_item_set_text(chunk_item, "SHUTDOWN chunk acknowledging up to TSN %u",
1549                         cumulative_tsn_ack);
1550   } 
1551 }
1552
1553 static void
1554 dissect_shutdown_ack_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
1555                            proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
1556
1557   if (check_col(pinfo->cinfo, COL_INFO))
1558     col_append_str(pinfo->cinfo, COL_INFO, "SHUTDOWN_ACK ");
1559
1560   if (chunk_tree) {
1561     proto_item_set_text(chunk_item, "SHUTDOWN ACK chunk");
1562   } 
1563 }
1564
1565 static void
1566 dissect_error_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
1567                     proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
1568
1569   guint offset, number_of_causes;
1570   guint16 length, padding_length, total_length;
1571   tvbuff_t *cause_tvb;
1572   
1573   if (check_col(pinfo->cinfo, COL_INFO))
1574     col_append_str(pinfo->cinfo, COL_INFO, "ERROR ");
1575
1576   if (chunk_tree) {
1577     number_of_causes = 0;
1578     offset = ERROR_CHUNK_FIRST_ERROR_CAUSE_OFFSET;
1579     do {
1580       length         = tvb_get_ntohs(chunk_tvb, offset + CAUSE_LENGTH_OFFSET);
1581       padding_length = nr_of_padding_bytes(length);
1582       total_length   = length + padding_length;
1583       /* create a tvb for the chunk including the padding bytes */
1584       cause_tvb      = tvb_new_subset(chunk_tvb, offset, total_length, total_length);
1585       dissect_error_cause(cause_tvb, pinfo, chunk_tree); 
1586       /* get rid of the handled parameter */
1587       offset += total_length;
1588       number_of_causes++;
1589     } while(tvb_length_remaining(chunk_tvb, offset));
1590     
1591     proto_item_set_text(chunk_item, "Error chunk with %u cause%s",
1592                         number_of_causes, plurality(number_of_causes, "", "s"));
1593   } 
1594 }
1595
1596 static void
1597 dissect_cookie_echo_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
1598                           proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
1599
1600   guint length, cookie_length, padding_length;
1601
1602   length         = tvb_get_ntohs(chunk_tvb, CHUNK_LENGTH_OFFSET);
1603   padding_length = nr_of_padding_bytes(length);
1604   cookie_length  = length - CHUNK_HEADER_LENGTH;
1605  
1606   if (check_col(pinfo->cinfo, COL_INFO))
1607     col_append_str(pinfo->cinfo, COL_INFO, "COOKIE_ECHO ");
1608
1609   if (chunk_tree) {  
1610     proto_tree_add_text(chunk_tree, chunk_tvb, COOKIE_ECHO_CHUNK_COOKIE_OFFSET, cookie_length,
1611                         "Cookie (%u byte%s)",
1612                         cookie_length, plurality(cookie_length, "", "s"));
1613     proto_item_set_text(chunk_item, "COOKIE ECHO chunk containing a cookie of %u byte%s",
1614                         cookie_length, plurality(cookie_length, "", "s"));
1615     
1616     if (padding_length > 0)
1617       proto_tree_add_text(chunk_tree, chunk_tvb, CHUNK_HEADER_OFFSET + length, padding_length,
1618                           "Padding: %u byte%s",
1619                           padding_length, plurality(padding_length, "", "s"));
1620   } 
1621 }
1622
1623 static void
1624 dissect_cookie_ack_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
1625                          proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
1626 {  
1627   if (check_col(pinfo->cinfo, COL_INFO))
1628     col_append_str(pinfo->cinfo, COL_INFO, "COOKIE_ACK ");
1629
1630   if (chunk_tree) {
1631     proto_item_set_text(chunk_item, "COOKIE ACK chunk");
1632   } 
1633 }
1634
1635 static void
1636 dissect_ecne_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
1637                    proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
1638
1639   guint32 lowest_tsn;
1640  
1641   if (check_col(pinfo->cinfo, COL_INFO))
1642     col_append_str(pinfo->cinfo, COL_INFO, "ECNE ");
1643
1644   if (chunk_tree) {
1645     lowest_tsn = tvb_get_ntohl(chunk_tvb, ECNE_CHUNK_LOWEST_TSN_OFFSET);
1646     proto_tree_add_uint(chunk_tree, hf_sctp_ecne_chunk_lowest_tsn, 
1647                         chunk_tvb,
1648                         ECNE_CHUNK_LOWEST_TSN_OFFSET, ECNE_CHUNK_LOWEST_TSN_LENGTH,
1649                         lowest_tsn);
1650     
1651     proto_item_set_text(chunk_item, "ECNE chunk");
1652   } 
1653 }
1654
1655 static void
1656 dissect_cwr_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
1657                   proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
1658
1659   guint32 lowest_tsn;
1660   
1661   if (check_col(pinfo->cinfo, COL_INFO))
1662     col_append_str(pinfo->cinfo, COL_INFO, "CWR ");
1663
1664   if (chunk_tree) {
1665     lowest_tsn = tvb_get_ntohl(chunk_tvb, CWR_CHUNK_LOWEST_TSN_OFFSET);
1666     proto_tree_add_uint(chunk_tree, hf_sctp_cwr_chunk_lowest_tsn, 
1667                         chunk_tvb,
1668                         CWR_CHUNK_LOWEST_TSN_OFFSET, CWR_CHUNK_LOWEST_TSN_LENGTH,
1669                         lowest_tsn);
1670     
1671     proto_item_set_text(chunk_item, "CWR chunk");
1672   } 
1673 }
1674
1675 static void
1676 dissect_shutdown_complete_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
1677                                 proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
1678
1679   guint8  flags;
1680   guint16 length;
1681   proto_tree *flag_tree;
1682  
1683   if (check_col(pinfo->cinfo, COL_INFO))
1684     col_append_str(pinfo->cinfo, COL_INFO, "SHUTDOWN_COMPLETE ");
1685
1686   if (chunk_tree) {
1687     flags             = tvb_get_guint8(chunk_tvb, CHUNK_FLAGS_OFFSET);
1688     length            = tvb_get_ntohs(chunk_tvb, CHUNK_LENGTH_OFFSET);
1689     
1690     flag_tree = proto_item_add_subtree(flags_item, ett_sctp_data_chunk_flags);
1691     proto_tree_add_boolean(flag_tree, hf_sctp_shutdown_complete_chunk_t_bit, chunk_tvb,
1692                            CHUNK_FLAGS_OFFSET, CHUNK_FLAGS_LENGTH, flags);
1693     
1694     proto_item_set_text(chunk_item, "SHUTDOWN COMPLETE chunk");
1695   } 
1696 }
1697
1698 #define FORWARD_TSN_CHUNK_TSN_OFFSET CHUNK_VALUE_OFFSET
1699 #define FORWARD_TSN_CHUNK_TSN_LENGTH 4
1700
1701 static void
1702 dissect_forward_tsn_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree, proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
1703
1704   guint32 tsn;
1705   
1706   if (check_col(pinfo->cinfo, COL_INFO))
1707     col_append_str(pinfo->cinfo, COL_INFO, "FORWARD TSN ");
1708
1709   if (chunk_tree) {
1710     tsn = tvb_get_ntohl(chunk_tvb, FORWARD_TSN_CHUNK_TSN_OFFSET);
1711     proto_tree_add_uint(chunk_tree, hf_sctp_forward_tsn_chunk_tsn, chunk_tvb, FORWARD_TSN_CHUNK_TSN_OFFSET, FORWARD_TSN_CHUNK_TSN_LENGTH, tsn);
1712     proto_item_set_text(chunk_item, "FORWARD TSN chunk (new cumulative TSN %u)", tsn);
1713   } 
1714 }
1715
1716 #define SERIAL_NUMBER_LENGTH    4
1717 #define CORRELATION_ID_LENGTH   4
1718 #define ASCONF_RESERVED_LENGTH  3
1719 #define ASCONF_ADDR_TYPE_LENGTH 1
1720 #define ASCONF_ADDR_LENGTH      16
1721 #define SERIAL_NUMBER_OFFSET    PARAMETER_VALUE_OFFSET
1722
1723 static void
1724 dissect_asconf_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree, proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
1725
1726   guint32 serial_number, correlation_id;
1727   guint offset, length, padding_length, total_length;
1728   guint8 addr_type;
1729   tvbuff_t *parameter_tvb;
1730
1731   if (check_col(pinfo->cinfo, COL_INFO))
1732     col_append_str(pinfo->cinfo, COL_INFO, "ASCONF ");
1733
1734   if (chunk_tree) {
1735     offset = SERIAL_NUMBER_OFFSET;
1736     serial_number    = tvb_get_ntohl(chunk_tvb, offset);
1737     proto_tree_add_uint(chunk_tree, hf_sctp_asconf_serial, chunk_tvb, offset, SERIAL_NUMBER_LENGTH, serial_number);
1738     offset          += SERIAL_NUMBER_LENGTH;
1739     proto_tree_add_bytes(chunk_tree, hf_sctp_asconf_reserved, chunk_tvb, offset, ASCONF_RESERVED_LENGTH, tvb_get_ptr(chunk_tvb, offset, ASCONF_RESERVED_LENGTH));
1740     offset          += ASCONF_RESERVED_LENGTH;
1741     addr_type = tvb_get_guint8(chunk_tvb, offset);
1742     proto_tree_add_uint(chunk_tree, hf_sctp_asconf_addr_type, chunk_tvb, offset, ASCONF_ADDR_TYPE_LENGTH, addr_type);
1743     offset          += ASCONF_ADDR_TYPE_LENGTH;
1744     proto_tree_add_bytes(chunk_tree, hf_sctp_asconf_addr, chunk_tvb, offset, ASCONF_ADDR_LENGTH, tvb_get_ptr(chunk_tvb, offset, ASCONF_ADDR_LENGTH));
1745     offset          += ASCONF_ADDR_LENGTH;
1746     proto_item_set_text(chunk_item, "ASCONF chunk");
1747     
1748     while(tvb_length_remaining(chunk_tvb, offset)) {
1749       correlation_id = tvb_get_ntohl(chunk_tvb, offset);
1750       proto_tree_add_uint(chunk_tree, hf_sctp_asconf_correlation_id, chunk_tvb, offset, CORRELATION_ID_LENGTH, correlation_id);
1751       offset        += CORRELATION_ID_LENGTH;
1752       length         = tvb_get_ntohs(chunk_tvb, offset + PARAMETER_LENGTH_OFFSET);
1753       padding_length = nr_of_padding_bytes(length);
1754       total_length   = length + padding_length;
1755       /* create a tvb for the chunk including the padding bytes */
1756       parameter_tvb  = tvb_new_subset(chunk_tvb, offset, total_length, total_length);
1757       dissect_parameter(parameter_tvb, pinfo, chunk_tree); 
1758       /* get rid of the handled parameter */
1759       offset        += total_length;
1760     }
1761   } 
1762 }
1763
1764 static void
1765 dissect_asconf_ack_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree, proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
1766
1767   guint32 serial_number, correlation_id;
1768   guint offset, length, padding_length, total_length;
1769   tvbuff_t *parameter_tvb;
1770
1771   if (check_col(pinfo->cinfo, COL_INFO))
1772     col_append_str(pinfo->cinfo, COL_INFO, "ASCONF-ACK ");
1773
1774   if (chunk_tree) {
1775     serial_number = tvb_get_ntohl(chunk_tvb, SERIAL_NUMBER_OFFSET);
1776     proto_tree_add_uint(chunk_tree, hf_sctp_asconf_ack_serial, chunk_tvb, SERIAL_NUMBER_OFFSET, SERIAL_NUMBER_LENGTH, serial_number);
1777     proto_item_set_text(chunk_item, "ASCONF-ACK chunk");
1778     
1779     offset = SERIAL_NUMBER_OFFSET + SERIAL_NUMBER_LENGTH;
1780     while(tvb_length_remaining(chunk_tvb, offset)) {
1781       correlation_id = tvb_get_ntohl(chunk_tvb, offset);
1782       proto_tree_add_uint(chunk_tree, hf_sctp_asconf_ack_correlation_id, chunk_tvb, offset, CORRELATION_ID_LENGTH, correlation_id);
1783       offset        += CORRELATION_ID_LENGTH;
1784       length         = tvb_get_ntohs(chunk_tvb, offset + PARAMETER_LENGTH_OFFSET);
1785       padding_length = nr_of_padding_bytes(length);
1786       total_length   = length + padding_length;
1787       /* create a tvb for the chunk including the padding bytes */
1788       parameter_tvb  = tvb_new_subset(chunk_tvb, offset, total_length, total_length);
1789       dissect_parameter(parameter_tvb, pinfo, chunk_tree); 
1790       /* get rid of the handled parameter */
1791       offset        += total_length;
1792     }
1793   } 
1794 }
1795
1796 static void
1797 dissect_unknown_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree,
1798                       proto_tree *chunk_tree, proto_item *chunk_item, proto_item *flags_item)
1799
1800   guint length, chunk_value_length, padding_length;
1801   guint8 type;
1802   
1803   if (check_col(pinfo->cinfo, COL_INFO))
1804     col_append_str(pinfo->cinfo, COL_INFO, "UNKNOWN ");
1805
1806   if (chunk_tree) {
1807     length         = tvb_get_ntohs(chunk_tvb, CHUNK_LENGTH_OFFSET);
1808     padding_length = nr_of_padding_bytes(length);
1809     type           = tvb_get_guint8(chunk_tvb, CHUNK_TYPE_OFFSET);
1810     
1811     chunk_value_length = length - CHUNK_HEADER_LENGTH;
1812     
1813     proto_tree_add_text(chunk_tree, chunk_tvb, CHUNK_VALUE_OFFSET, chunk_value_length,
1814                         "Chunk value (%u byte%s)",
1815                         chunk_value_length, plurality(chunk_value_length, "", "s"));
1816     
1817     if (padding_length > 0)
1818       proto_tree_add_text(chunk_tree, chunk_tvb, CHUNK_HEADER_OFFSET + length, padding_length,
1819                           "Padding: %u byte%s",
1820                           padding_length, plurality(padding_length, "", "s"));
1821     
1822     proto_item_set_text(chunk_item, "Chunk of type %u and %u byte%s value",
1823                         type, chunk_value_length, plurality(chunk_value_length, "", "s"));
1824   }
1825
1826
1827
1828 static gboolean
1829 dissect_sctp_chunk(tvbuff_t *chunk_tvb, packet_info *pinfo, proto_tree *tree, proto_tree *sctp_tree)
1830 {  
1831   guint8 type, flags;
1832   guint16 length;
1833   gboolean result;
1834   proto_item *flags_item;
1835   proto_item *chunk_item;
1836   proto_tree *chunk_tree;
1837
1838   result = FALSE;
1839   
1840   /* first extract the chunk header */
1841   type   = tvb_get_guint8(chunk_tvb, CHUNK_TYPE_OFFSET);
1842   flags  = tvb_get_guint8(chunk_tvb, CHUNK_FLAGS_OFFSET);
1843   length = tvb_get_ntohs(chunk_tvb, CHUNK_LENGTH_OFFSET);
1844  
1845   if (tree) {
1846     /* create proto_tree stuff */
1847     chunk_item   = proto_tree_add_text(sctp_tree, chunk_tvb, CHUNK_HEADER_OFFSET, tvb_length(chunk_tvb), "Incomplete chunk");
1848     chunk_tree   = proto_item_add_subtree(chunk_item, ett_sctp_chunk);
1849     
1850     /* then insert the chunk header components into the protocol tree */
1851     proto_tree_add_uint(chunk_tree, hf_sctp_chunk_type, chunk_tvb, CHUNK_TYPE_OFFSET, CHUNK_TYPE_LENGTH, type);
1852     flags_item = proto_tree_add_uint(chunk_tree, hf_sctp_chunk_flags, chunk_tvb, CHUNK_FLAGS_OFFSET, CHUNK_FLAGS_LENGTH, flags);
1853     proto_tree_add_uint(chunk_tree, hf_sctp_chunk_length, chunk_tvb, CHUNK_LENGTH_OFFSET, CHUNK_LENGTH_LENGTH, length);
1854   } else {
1855     chunk_tree = NULL;
1856     chunk_item = NULL;
1857     flags_item = NULL;
1858   };
1859   
1860   /* now dissect the chunk value */
1861
1862   switch(type) {
1863   case SCTP_DATA_CHUNK_ID:
1864     result = dissect_data_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
1865     break;
1866   case SCTP_INIT_CHUNK_ID:
1867     dissect_init_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
1868     break;
1869   case SCTP_INIT_ACK_CHUNK_ID:
1870     dissect_init_ack_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
1871     break;
1872   case SCTP_SACK_CHUNK_ID:
1873     dissect_sack_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
1874     break; 
1875   case SCTP_HEARTBEAT_CHUNK_ID:
1876     dissect_heartbeat_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
1877     break;
1878   case SCTP_HEARTBEAT_ACK_CHUNK_ID:
1879     dissect_heartbeat_ack_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
1880     break;
1881   case SCTP_ABORT_CHUNK_ID:
1882     dissect_abort_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
1883     break;
1884   case SCTP_SHUTDOWN_CHUNK_ID:
1885     dissect_shutdown_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
1886     break;
1887   case SCTP_SHUTDOWN_ACK_CHUNK_ID:
1888     dissect_shutdown_ack_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
1889     break;
1890   case SCTP_ERROR_CHUNK_ID:
1891     dissect_error_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
1892     break;
1893   case SCTP_COOKIE_ECHO_CHUNK_ID:
1894     dissect_cookie_echo_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
1895     break;
1896   case SCTP_COOKIE_ACK_CHUNK_ID:
1897     dissect_cookie_ack_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
1898     break;
1899   case SCTP_ECNE_CHUNK_ID:
1900     dissect_ecne_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
1901     break;
1902   case SCTP_CWR_CHUNK_ID:
1903     dissect_cwr_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
1904     break;
1905   case SCTP_SHUTDOWN_COMPLETE_CHUNK_ID:
1906     dissect_shutdown_complete_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
1907     break;
1908   case SCTP_FORWARD_TSN_CHUNK_ID:
1909     dissect_forward_tsn_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
1910     break;
1911   case SCTP_ASCONF_ACK_CHUNK_ID:
1912     dissect_asconf_ack_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
1913     break;
1914   case SCTP_ASCONF_CHUNK_ID:
1915     dissect_asconf_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
1916     break;
1917   default:
1918     dissect_unknown_chunk(chunk_tvb, pinfo, tree, chunk_tree, chunk_item, flags_item);
1919     break;
1920   };
1921   return result;
1922 }
1923
1924 static void
1925 dissect_sctp_chunks(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, proto_item *sctp_item, proto_tree *sctp_tree)
1926
1927   tvbuff_t *chunk_tvb;
1928   guint16 length, padding_length, total_length;
1929   gint last_offset, offset;
1930   gboolean sctp_item_length_set;
1931
1932   /* the common header of the datagram is already handled */
1933   last_offset = 0;
1934   offset = COMMON_HEADER_LENGTH;
1935   sctp_item_length_set = FALSE;
1936
1937   while(tvb_length_remaining(tvb, offset) > 0) {
1938     /* extract the chunk length and compute number of padding bytes */
1939     length         = tvb_get_ntohs(tvb, offset + CHUNK_LENGTH_OFFSET);
1940     padding_length = nr_of_padding_bytes(length);
1941     total_length   = length + padding_length;
1942     /* create a tvb for the chunk including the padding bytes */
1943     chunk_tvb    = tvb_new_subset(tvb, offset, total_length, total_length);
1944     /* call dissect_sctp_chunk for a actual work */
1945     if (dissect_sctp_chunk(chunk_tvb, pinfo, tree, sctp_tree) && (tree)) {
1946       proto_item_set_len(sctp_item, offset - last_offset + DATA_CHUNK_HEADER_LENGTH);
1947       sctp_item_length_set = TRUE;
1948       offset += total_length;
1949       last_offset = offset;
1950       if (tvb_length_remaining(tvb, offset) > 0) {
1951         sctp_item = proto_tree_add_item(tree, proto_sctp, tvb, offset, 0, FALSE);
1952         sctp_tree = proto_item_add_subtree(sctp_item, ett_sctp);
1953         sctp_item_length_set = FALSE;
1954       }
1955     } else {
1956     /* get rid of the dissected chunk */
1957     offset += total_length;
1958     }
1959   };
1960   if (!sctp_item_length_set && (tree)) { 
1961     proto_item_set_len(sctp_item, offset - last_offset);
1962   };
1963 }
1964
1965 /* dissect_sctp handles the common header of a SCTP datagram.
1966  * For the handling of the chunks dissect_sctp_chunks is called.
1967  */
1968
1969 static void
1970 dissect_sctp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1971 {
1972   guint16 source_port, destination_port;
1973   guint32 verification_tag, checksum, calculated_checksum;
1974   guint length;
1975   gboolean checksum_correct;
1976   proto_item *sctp_item;
1977   proto_tree *sctp_tree;
1978
1979   /* Extract the common header */
1980   source_port      = tvb_get_ntohs(tvb, SOURCE_PORT_OFFSET);
1981   destination_port = tvb_get_ntohs(tvb, DESTINATION_PORT_OFFSET);
1982   verification_tag = tvb_get_ntohl(tvb, VERIFICATION_TAG_OFFSET);
1983   checksum         = tvb_get_ntohl(tvb, CHECKSUM_OFFSET);
1984
1985   /* update pi structure */
1986   pinfo->ptype    = PT_SCTP;
1987   pinfo->srcport  = source_port;
1988   pinfo->destport = destination_port;
1989
1990   /* make entry in the Protocol column on summary display */
1991   if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
1992     col_set_str(pinfo->cinfo, COL_PROTOCOL, "SCTP");
1993
1994   /* Clear entries in Info column on summary display */
1995   if (check_col(pinfo->cinfo, COL_INFO))
1996     col_add_str(pinfo->cinfo, COL_INFO, "");
1997   
1998   /* In the interest of speed, if "tree" is NULL, don't do any work not
1999      necessary to generate protocol tree items. */
2000   if (tree) {
2001     /* create the sctp protocol tree */
2002     sctp_item = proto_tree_add_item(tree, proto_sctp, tvb, 0, 0, FALSE);
2003     sctp_tree = proto_item_add_subtree(sctp_item, ett_sctp);
2004
2005     /* add the components of the common header to the protocol tree */
2006     proto_tree_add_uint(sctp_tree, hf_sctp_source_port, tvb, SOURCE_PORT_OFFSET, SOURCE_PORT_LENGTH, source_port);
2007     proto_tree_add_uint(sctp_tree, hf_sctp_destination_port, tvb, DESTINATION_PORT_OFFSET, DESTINATION_PORT_LENGTH, destination_port);
2008     proto_tree_add_uint(sctp_tree, hf_sctp_verification_tag, tvb, VERIFICATION_TAG_OFFSET, VERIFICATION_TAG_LENGTH, verification_tag);
2009   
2010     length = tvb_length(tvb);
2011     calculated_checksum = sctp_adler_32(tvb_get_ptr(tvb, 0, length), length);
2012     checksum_correct = (checksum == calculated_checksum);
2013     if (checksum_correct)
2014       proto_tree_add_uint_format(sctp_tree, hf_sctp_checksum, tvb, CHECKSUM_OFFSET, CHECKSUM_LENGTH, checksum, 
2015                                  "Adler-32 checksum: 0x%08x (correct)", checksum);
2016     else {
2017       proto_tree_add_uint_format(sctp_tree, hf_sctp_checksum, tvb, CHECKSUM_OFFSET, CHECKSUM_LENGTH, checksum,
2018                                  "Adler-32 checksum: 0x%08x (incorrect, should be 0x%08x)", checksum, calculated_checksum);    
2019       proto_tree_add_boolean_hidden(sctp_tree, hf_sctp_checksum_correct, tvb, CHECKSUM_OFFSET, CHECKSUM_LENGTH, checksum_correct);
2020     };
2021   } else {
2022     sctp_tree = NULL;
2023     sctp_item = NULL;
2024   };
2025   /* add all chunks of the sctp datagram to the protocol tree */
2026   dissect_sctp_chunks(tvb, pinfo, tree, sctp_item, sctp_tree);
2027 }
2028
2029 /* Register the protocol with Ethereal */
2030 void
2031 proto_register_sctp(void)
2032 {                 
2033
2034   /* Setup list of header fields */
2035   static hf_register_info hf[] = {
2036     { &hf_sctp_source_port,
2037       { "Source port", "sctp.srcport",
2038               FT_UINT16, BASE_DEC, NULL, 0x0,          
2039               "", HFILL }
2040     },
2041     { &hf_sctp_destination_port,
2042       { "Destination port", "sctp.dstport",
2043                FT_UINT16, BASE_DEC, NULL, 0x0,          
2044                "", HFILL }
2045     }, 
2046     { &hf_sctp_verification_tag,
2047       { "Verification tag", "sctp.verfication_tag",
2048                FT_UINT32, BASE_HEX, NULL, 0x0,          
2049                "", HFILL }
2050     },
2051     { &hf_sctp_checksum,
2052       { "Adler-32 checksum", "sctp.checksum",
2053               FT_UINT32, BASE_HEX, NULL, 0x0,          
2054               "", HFILL }
2055     },
2056     { &hf_sctp_checksum_correct,
2057       { "Adler-32 checksum correct", "sctp.checksum_correct",
2058               FT_BOOLEAN, BASE_NONE, NULL, 0x0,          
2059               "", HFILL }
2060     },
2061     { &hf_sctp_chunk_type,
2062       { "Identifier", "sctp.chunk_type",
2063         FT_UINT8, BASE_DEC, VALS(sctp_chunk_type_values), 0x0,          
2064         "", HFILL }
2065     },
2066     { &hf_sctp_chunk_flags,
2067       { "Flags", "sctp.chunk_flags",
2068         FT_UINT8, BASE_BIN, NULL, 0x0,          
2069         "", HFILL }
2070     },
2071     { &hf_sctp_chunk_length,
2072       { "Length", "sctp.chunk_length",
2073         FT_UINT16, BASE_DEC, NULL, 0x0,          
2074         "", HFILL }
2075     },
2076     { &hf_sctp_init_chunk_initiate_tag,
2077       { "Initiate tag", "sctp.init.chunk.initiate.tag",
2078         FT_UINT32, BASE_HEX, NULL, 0x0,          
2079         "", HFILL }
2080     },
2081     { &hf_sctp_init_chunk_adv_rec_window_credit,
2082       { "Advertised reciever window credit (a_rwnd)", "sctp.init.chunk.credit",
2083         FT_UINT32, BASE_DEC, NULL, 0x0,          
2084         "", HFILL }
2085     },
2086     { &hf_sctp_init_chunk_number_of_outbound_streams,
2087       { "Number of outbound streams", "sctp.init.chunk.nr.out.streams",
2088         FT_UINT16, BASE_DEC, NULL, 0x0,          
2089         "", HFILL }
2090     },
2091     { &hf_sctp_init_chunk_number_of_inbound_streams,
2092       { "Number of inbound streams", "sctp.init.chunk.nr.in.streams",
2093         FT_UINT16, BASE_DEC, NULL, 0x0,          
2094         "", HFILL }
2095     },
2096     {&hf_sctp_init_chunk_initial_tsn,
2097       { "Initial TSN", "sctp.init.chunk.initial.tsn",
2098         FT_UINT32, BASE_DEC, NULL, 0x0,          
2099         "", HFILL }
2100     }, 
2101     {&hf_sctp_cumulative_tsn_ack,
2102      { "Cumulative TSN Ack", "sctp.cumulative.tsn.ack",
2103         FT_UINT32, BASE_DEC, NULL, 0x0,          
2104         "", HFILL }
2105     },
2106     {&hf_sctp_data_chunk_tsn,
2107      { "TSN", "sctp.tsn",
2108        FT_UINT32, BASE_DEC, NULL, 0x0,          
2109         "", HFILL }
2110     },
2111     {&hf_sctp_data_chunk_stream_id,
2112      { "Stream Identifier", "sctp.stream_id",
2113         FT_UINT16, BASE_HEX, NULL, 0x0,          
2114         "", HFILL }
2115     },
2116     {&hf_sctp_data_chunk_stream_seq_number,
2117      { "Stream sequence number", "sctp.stream_seq_number",
2118        FT_UINT16, BASE_DEC, NULL, 0x0,          
2119        "", HFILL }
2120     },
2121     {&hf_sctp_data_chunk_payload_proto_id,
2122      { "Payload protocol identifier", "sctp.payload_proto_id",
2123         FT_UINT32, BASE_DEC, VALS(sctp_payload_proto_id_values), 0x0,          
2124         "", HFILL }
2125     },
2126     {&hf_sctp_data_chunk_e_bit,
2127      { "E-Bit", "sctp.data.e_bit",
2128        FT_BOOLEAN, 8, TFS(&sctp_data_chunk_e_bit_value), SCTP_DATA_CHUNK_E_BIT,          
2129        "", HFILL }
2130     },
2131     {&hf_sctp_data_chunk_b_bit,
2132      { "B-Bit", "sctp.data.b_bit",
2133        FT_BOOLEAN, 8, TFS(&sctp_data_chunk_b_bit_value), SCTP_DATA_CHUNK_B_BIT,          
2134        "", HFILL }
2135     },
2136     {&hf_sctp_data_chunk_u_bit,
2137      { "U-Bit", "sctp.data.u.bit",
2138        FT_BOOLEAN, 8, TFS(&sctp_data_chunk_u_bit_value), SCTP_DATA_CHUNK_U_BIT,          
2139        "", HFILL }
2140     },
2141     {&hf_sctp_sack_chunk_cumulative_tsn_ack,
2142      { "Cumulative TSN ACK", "sctp.sack.cumulative_tsn_ack",
2143        FT_UINT32, BASE_DEC, NULL, 0x0,          
2144        "", HFILL }
2145     }, 
2146     {&hf_sctp_sack_chunk_adv_rec_window_credit,
2147      { "Advertised receiver window credit (a_rwnd)", "sctp.sack.a_rwnd",
2148        FT_UINT32, BASE_DEC, NULL, 0x0,          
2149        "", HFILL }
2150     },
2151     {&hf_sctp_sack_chunk_number_of_gap_blocks,
2152      { "Number of gap acknowldgement blocks ", "sctp.sack.number_of_gap_blocks",
2153        FT_UINT16, BASE_DEC, NULL, 0x0,          
2154        "", HFILL }
2155     }, 
2156     {&hf_sctp_sack_chunk_number_of_dup_tsns,
2157      { "Number of duplicated TSNs", "sctp.sack.number_of_duplicated_tsns",
2158        FT_UINT16, BASE_DEC, NULL, 0x0,          
2159        "", HFILL }
2160     },
2161     {&hf_sctp_sack_chunk_gap_block_start,
2162      { "Start", "sctp.sack.gap_block_start",
2163        FT_UINT16, BASE_DEC, NULL, 0x0,          
2164        "", HFILL }
2165     },
2166     {&hf_sctp_sack_chunk_gap_block_end,
2167      { "End", "sctp.sack.gap_block_end",
2168        FT_UINT16, BASE_DEC, NULL, 0x0,          
2169        "", HFILL }
2170     },
2171     {&hf_sctp_sack_chunk_duplicate_tsn,
2172      { "Duplicate TSN", "sctp.sack.duplicate.tsn",
2173        FT_UINT16, BASE_DEC, NULL, 0x0,          
2174        "", HFILL }
2175     },  
2176     {&hf_sctp_shutdown_chunk_cumulative_tsn_ack,
2177      { "Cumulative TSN Ack", "sctp.shutdown.cumulative_tsn_ack",
2178         FT_UINT32, BASE_DEC, NULL, 0x0,          
2179         "", HFILL }
2180     },
2181     {&hf_sctp_ecne_chunk_lowest_tsn,
2182      { "Lowest TSN", "sctp.ecne.lowest_tsn",
2183        FT_UINT32, BASE_DEC, NULL, 0x0,          
2184        "", HFILL }
2185     }, 
2186     {&hf_sctp_cwr_chunk_lowest_tsn,
2187      { "Lowest TSN", "sctp.cwr.lowest_tsn",
2188        FT_UINT32, BASE_DEC, NULL, 0x0,          
2189        "", HFILL }
2190     }, 
2191     {&hf_sctp_shutdown_complete_chunk_t_bit,
2192      { "E-Bit", "sctp.shutdown_complete.t_bit",
2193        FT_BOOLEAN, 8, TFS(&sctp_shutdown_complete_chunk_t_bit_value), SCTP_SHUTDOWN_COMPLETE_CHUNK_T_BIT,
2194        "", HFILL }
2195     },
2196     {&hf_sctp_forward_tsn_chunk_tsn,
2197      { "New cumulative TSN", "sctp.forward_tsn.tsn",
2198        FT_UINT32, BASE_DEC, NULL, 0x0,          
2199        "", HFILL }
2200     }, 
2201     {&hf_sctp_chunk_parameter_type,
2202      { "Parameter type", "sctp.parameter.type",
2203        FT_UINT16, BASE_HEX, VALS(sctp_parameter_identifier_values), 0x0,
2204        "", HFILL }
2205     },
2206     {&hf_sctp_chunk_parameter_length,
2207      { "Parameter length", "sctp.parameter.length",
2208        FT_UINT16, BASE_DEC, NULL, 0x0,          
2209        "", HFILL }
2210     },
2211     {&hf_sctp_parameter_ipv4_address,
2212      { "IP Version 4 address", "sctp.parameter.ipv4_address",
2213        FT_IPv4, BASE_NONE, NULL, 0x0,
2214        "", HFILL }
2215     },
2216     {&hf_sctp_parameter_ipv6_address,
2217      { "IP Version 6 address", "sctp.parameter.ipv6_address",
2218        FT_IPv6, BASE_NONE, NULL, 0x0,
2219        "", HFILL }
2220     },
2221     {&hf_sctp_parameter_cookie_preservative_increment,
2222      { "Suggested Cookie life-span increment (msec)", "sctp.parameter.cookie_preservative_incr",
2223        FT_UINT32, BASE_DEC, NULL, 0x0,          
2224        "", HFILL }
2225     },
2226     {&hf_sctp_parameter_hostname_hostname,
2227      { "Hostname", "sctp.parameter.hostname.hostname",
2228        FT_STRING, BASE_NONE, NULL, 0x0,          
2229        "", HFILL }
2230     }, 
2231     {&hf_sctp_supported_address_types_parameter,
2232      { "Supported address type", "sctp.parameter.supported_addres_type",
2233        FT_UINT16, BASE_DEC, NULL, 0x0,          
2234        "", HFILL }
2235     }, 
2236     {&hf_sctp_ustreams_start,
2237      { "Start", "sctp.unreliable_streams.start",
2238        FT_UINT16, BASE_DEC, NULL, 0x0,          
2239        "", HFILL }
2240     }, 
2241     {&hf_sctp_ustreams_end,
2242      { "End", "sctp.unreliable_streams.end",
2243        FT_UINT16, BASE_DEC, NULL, 0x0,          
2244        "", HFILL }
2245     },     
2246     {&hf_sctp_asconf_serial,
2247      { "Serial Number", "sctp.asconf.serial_number",
2248        FT_UINT32, BASE_HEX, NULL, 0x0,          
2249        "", HFILL }
2250     }, 
2251     {&hf_sctp_asconf_correlation_id,
2252      { "Correlation_id", "sctp.asconf.correlation_id",
2253        FT_UINT32, BASE_HEX, NULL, 0x0,          
2254        "", HFILL }
2255     }, 
2256     {&hf_sctp_asconf_reserved,
2257      { "Reserved", "sctp.asconf.reserved",
2258        FT_BYTES, BASE_NONE, NULL, 0x0,          
2259        "", HFILL }
2260     }, 
2261     {&hf_sctp_asconf_addr_type,
2262      { "Address type", "sctp.asconf.address_type",
2263        FT_UINT8, BASE_HEX, NULL, 0x0,          
2264        "", HFILL }
2265     }, 
2266     {&hf_sctp_asconf_addr,
2267      { "Address bytes", "sctp.asconf.address_bytes",
2268        FT_BYTES, BASE_NONE, NULL, 0x0,          
2269        "", HFILL }
2270     }, 
2271     {&hf_sctp_asconf_ack_serial,
2272      { "Serial Number", "sctp.asconf_ack.serial_number",
2273        FT_UINT32, BASE_HEX, NULL, 0x0,          
2274        "", HFILL }
2275     }, 
2276     {&hf_sctp_asconf_ack_correlation_id,
2277      { "Correlation_id", "sctp.asconf_ack.correlation_id",
2278        FT_UINT32, BASE_HEX, NULL, 0x0,          
2279        "", HFILL }
2280     },
2281     {&hf_sctp_adap_indication,
2282      { "Indication", "sctp.adapation_layer_indication.indication",
2283        FT_UINT32, BASE_HEX, NULL, 0x0,          
2284        "", HFILL }
2285     }, 
2286     {&hf_sctp_cause_code,
2287      { "Cause code", "sctp.cause.code",
2288        FT_UINT16, BASE_HEX, VALS(sctp_cause_code_values), 0x0,          
2289        "", HFILL }
2290     },
2291     {&hf_sctp_cause_length,
2292      { "Cause length", "sctp.cause.length",
2293        FT_UINT16, BASE_DEC, NULL, 0x0,          
2294        "", HFILL }
2295     }, 
2296     {&hf_sctp_cause_stream_identifier,
2297      { "Stream identifier", "sctp.cause.stream_identifier",
2298        FT_UINT16, BASE_DEC, NULL, 0x0,          
2299        "", HFILL }
2300     },
2301     {&hf_sctp_cause_number_of_missing_parameters,
2302      { "Number of missing parameters", "sctp.cause.nr_of_missing_parameters",
2303        FT_UINT32, BASE_DEC, NULL, 0x0,          
2304        "", HFILL }
2305     }, 
2306     {&hf_sctp_cause_missing_parameter_type,
2307      { "Missing parameter type", "sctp.cause.missing_parameter_type",
2308        FT_UINT16, BASE_HEX, VALS(sctp_parameter_identifier_values), 0x0,
2309        "", HFILL }
2310     },
2311     {&hf_sctp_cause_measure_of_staleness,
2312      { "Measure of staleness in usec", "sctp.cause.measure_of_staleness",
2313        FT_UINT32, BASE_DEC, NULL, 0x0,          
2314        "", HFILL }
2315     },
2316     {&hf_sctp_cause_tsn,
2317      { "TSN", "sctp.cause.tsn",
2318        FT_UINT32, BASE_DEC, NULL, 0x0,          
2319        "", HFILL }
2320     },
2321   };
2322   
2323   /* Setup protocol subtree array */
2324   static gint *ett[] = {
2325     &ett_sctp,
2326     &ett_sctp_chunk,
2327     &ett_sctp_chunk_parameter,
2328     &ett_sctp_chunk_cause,
2329     &ett_sctp_data_chunk_flags,
2330     &ett_sctp_sack_chunk_gap_block,
2331     &ett_sctp_supported_address_types_parameter,
2332     &ett_sctp_unrecognized_parameter_parameter,
2333     &ett_sctp_unreliable_streams_interval
2334   };
2335   
2336   /* Register the protocol name and description */
2337   proto_sctp = proto_register_protocol("Stream Control Transmission Protocol",
2338                                        "SCTP", "sctp");
2339   
2340   /* Required function calls to register the header fields and subtrees used */
2341   proto_register_field_array(proto_sctp, hf, array_length(hf));
2342   proto_register_subtree_array(ett, array_length(ett));
2343
2344   /* subdissector code */
2345   sctp_port_dissector_table = register_dissector_table("sctp.port",
2346         "SCTP port", FT_UINT16, BASE_DEC);
2347   sctp_ppi_dissector_table  = register_dissector_table("sctp.ppi",
2348         "SCTP payload protocol identifier", FT_UINT32, BASE_HEX);
2349
2350 };
2351
2352 void
2353 proto_reg_handoff_sctp(void)
2354 {
2355   dissector_handle_t sctp_handle;
2356
2357   sctp_handle = create_dissector_handle(dissect_sctp, proto_sctp);
2358   dissector_add("ip.proto", IP_PROTO_SCTP, sctp_handle);
2359 }