summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-m2tp.c
blob: 59814f71a4857255bdfcd64bc4b99cb0fd8f7ce2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/* packet-m2tp.c
 * Routines for M2TP User Adaptation Layer dissection
 * M2TP - MTP2 Transparent Proxy - is a Radisys proprietary
 * protocol based on the IETF SIGTRAN standard
 *
 * Copyright 2001, Heinz Prantner <heinz.prantner[AT]radisys.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * Copied from packet-m3ua.c
 * Thanks to Michael Tuexen for his valuable improvements
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <epan/packet.h>

#include <wsutil/str_util.h>

#define M2TP_PAYLOAD_PROTOCOL_ID                       99    /* s-link, not IANA-registered */

void proto_register_m2tp(void);
void proto_reg_handoff_m2tp(void);

static dissector_handle_t m2tp_handle;

#define SCTP_PORT_M2TP        9908  /* unassigned port number (not assigned by IANA) */

#define VERSION_LENGTH         1
#define RESERVED_LENGTH        1
#define MESSAGE_CLASS_LENGTH   1
#define MESSAGE_TYPE_LENGTH    1
#define MESSAGE_LENGTH_LENGTH  4
#define COMMON_HEADER_LENGTH   (VERSION_LENGTH + RESERVED_LENGTH + MESSAGE_CLASS_LENGTH + \
                                MESSAGE_TYPE_LENGTH + MESSAGE_LENGTH_LENGTH)

#define VERSION_OFFSET         0
#define RESERVED_OFFSET        (VERSION_OFFSET + VERSION_LENGTH)
#define MESSAGE_CLASS_OFFSET   (RESERVED_OFFSET + RESERVED_LENGTH)
#define MESSAGE_TYPE_OFFSET    (MESSAGE_CLASS_OFFSET + MESSAGE_CLASS_LENGTH)
#define MESSAGE_LENGTH_OFFSET  (MESSAGE_TYPE_OFFSET + MESSAGE_TYPE_LENGTH)

#define PARAMETER_TAG_LENGTH    2
#define PARAMETER_LENGTH_LENGTH 2
#define PARAMETER_HEADER_LENGTH (PARAMETER_TAG_LENGTH + PARAMETER_LENGTH_LENGTH)

#define PARAMETER_TAG_OFFSET      0
#define PARAMETER_LENGTH_OFFSET   (PARAMETER_TAG_OFFSET + PARAMETER_TAG_LENGTH)
#define PARAMETER_VALUE_OFFSET    (PARAMETER_LENGTH_OFFSET + PARAMETER_LENGTH_LENGTH)
#define PARAMETER_HEADER_OFFSET   PARAMETER_TAG_OFFSET

#define INTERFACE_IDENTIFIER_PARAMETER_TAG     1
#define MASTER_SLAVE_INDICATOR_PARAMETER_TAG   2
#define M2TP_USER_IDENTIFIER_PARAMETER_TAG     3
#define INFO_PARAMETER_TAG                     4
#define DIAGNOSTIC_INFORMATION_PARAMETER_TAG   7
#define HEARTBEAT_DATA_PARAMETER_TAG           9
#define REASON_PARAMETER_TAG                  10
#define ERROR_CODE_PARAMETER_TAG              12
#define PROTOCOL_DATA_PARAMETER_TAG           13


static const value_string m2tp_parameter_tag_values[] = {
  { INTERFACE_IDENTIFIER_PARAMETER_TAG,         "Interface Identifier" },
  { MASTER_SLAVE_INDICATOR_PARAMETER_TAG,       "Master Slave Indicator" },
  { M2TP_USER_IDENTIFIER_PARAMETER_TAG,         "M2tp User Identifier" },
  { INFO_PARAMETER_TAG,                         "Info" },
  { DIAGNOSTIC_INFORMATION_PARAMETER_TAG,       "Diagnostic Information" },
  { HEARTBEAT_DATA_PARAMETER_TAG,               "Heartbeat Data" },
  { REASON_PARAMETER_TAG,                       "Reason" },
  { ERROR_CODE_PARAMETER_TAG,                   "Error Code" },
  { PROTOCOL_DATA_PARAMETER_TAG,                "Protocol Data" },
  { 0,                           NULL } };

#define PROTOCOL_VERSION_RELEASE_1             1

static const value_string m2tp_protocol_version_values[] = {
  { PROTOCOL_VERSION_RELEASE_1,  "Release 1" },
  { 0,                           NULL } };

#define MESSAGE_CLASS_MGMT_MESSAGE        0
#define MESSAGE_CLASS_SGSM_MESSAGE        3
#define MESSAGE_CLASS_MAUP_MESSAGE        6
#define MESSAGE_CLASS_DATA_MESSAGE        255

static const value_string m2tp_message_class_values[] = {
  { MESSAGE_CLASS_MGMT_MESSAGE,   "Management Messages" },
  { MESSAGE_CLASS_SGSM_MESSAGE,   "SG State Maintenance Messages" },
  { MESSAGE_CLASS_MAUP_MESSAGE,   "MTP2 User Adaptation Messages" },
  { MESSAGE_CLASS_DATA_MESSAGE,   "User Data Messages" },
  { 0,                            NULL } };

/* management messages */
#define MESSAGE_TYPE_ERR                  0

/* sg state maintenance messages */
#define MESSAGE_TYPE_UP                   1
#define MESSAGE_TYPE_DOWN                 2
#define MESSAGE_TYPE_BEAT                 3
#define MESSAGE_TYPE_UP_ACK               4
#define MESSAGE_TYPE_DOWN_ACK             5
#define MESSAGE_TYPE_BEAT_ACK             6

/* mtp2 user message */
#define MESSAGE_TYPE_DATA                 1


static const value_string m2tp_message_class_type_values[] = {
  { MESSAGE_CLASS_MGMT_MESSAGE  * 256 + MESSAGE_TYPE_ERR,           "Error (ERR)" },
  { MESSAGE_CLASS_DATA_MESSAGE  * 256 + MESSAGE_TYPE_DATA,          "Payload data (DATA)" },
  { MESSAGE_CLASS_SGSM_MESSAGE  * 256 + MESSAGE_TYPE_UP,            "ASP up (UP)" },
  { MESSAGE_CLASS_SGSM_MESSAGE  * 256 + MESSAGE_TYPE_DOWN,          "ASP down (DOWN)" },
  { MESSAGE_CLASS_SGSM_MESSAGE  * 256 + MESSAGE_TYPE_BEAT,          "Heartbeat (BEAT)" },
  { MESSAGE_CLASS_SGSM_MESSAGE  * 256 + MESSAGE_TYPE_UP_ACK,        "ASP up ack (UP ACK)" },
  { MESSAGE_CLASS_SGSM_MESSAGE  * 256 + MESSAGE_TYPE_DOWN_ACK,      "ASP down ack (DOWN ACK)" },
  { MESSAGE_CLASS_SGSM_MESSAGE  * 256 + MESSAGE_TYPE_BEAT_ACK,      "Heartbeat ack (BEAT ACK)" },
  { 0,                           NULL } };

static const value_string m2tp_message_class_type_acro_values[] = {
  { MESSAGE_CLASS_MGMT_MESSAGE  * 256 + MESSAGE_TYPE_ERR,           "ERR" },
  { MESSAGE_CLASS_DATA_MESSAGE  * 256 + MESSAGE_TYPE_DATA,          "DATA" },
  { MESSAGE_CLASS_SGSM_MESSAGE  * 256 + MESSAGE_TYPE_UP,            "ASP_UP" },
  { MESSAGE_CLASS_SGSM_MESSAGE  * 256 + MESSAGE_TYPE_DOWN,          "ASP_DOWN" },
  { MESSAGE_CLASS_SGSM_MESSAGE  * 256 + MESSAGE_TYPE_BEAT,          "BEAT" },
  { MESSAGE_CLASS_SGSM_MESSAGE  * 256 + MESSAGE_TYPE_UP_ACK,        "ASP_UP_ACK" },
  { MESSAGE_CLASS_SGSM_MESSAGE  * 256 + MESSAGE_TYPE_DOWN_ACK,      "ASP_DOWN_ACK" },
  { MESSAGE_CLASS_SGSM_MESSAGE  * 256 + MESSAGE_TYPE_BEAT_ACK,      "BEAT_ACK" },
  { 0,                           NULL } };



#define HEARTBEAT_PERIOD_OFFSET PARAMETER_VALUE_OFFSET

#define INTERFACE_IDENTIFIER_LENGTH 4
#define INTERFACE_IDENTIFIER_OFFSET PARAMETER_VALUE_OFFSET

#define M2TP_USER_LENGTH 4
#define M2TP_USER_OFFSET PARAMETER_VALUE_OFFSET

#define PROTOCOL_DATA_OFFSET PARAMETER_VALUE_OFFSET

#define MASTER_SLAVE_LENGTH 4
#define MASTER_SLAVE_OFFSET PARAMETER_VALUE_OFFSET

#define REASON_LENGTH 4
#define REASON_OFFSET PARAMETER_VALUE_OFFSET

#define HEART_BEAT_DATA_OFFSET PARAMETER_VALUE_OFFSET

#define ERROR_CODE_LENGTH 4
#define ERROR_CODE_OFFSET PARAMETER_VALUE_OFFSET

#define INFO_STRING_OFFSET PARAMETER_VALUE_OFFSET

#define BSN_OFFSET PARAMETER_VALUE_OFFSET
#define FSN_OFFSET PARAMETER_VALUE_OFFSET+1

#define M2TP_USER_MTP2          1
#define M2TP_USER_Q921          2
#define M2TP_USER_FRAME_RELAY   3

static const value_string m2tp_user_identifier_values[] = {
  { M2TP_USER_MTP2,        "MTP2" },
  { M2TP_USER_Q921,        "Q.921" },
  { M2TP_USER_FRAME_RELAY, "Frame Relay" },
  { 0, NULL }};

#define M2TP_MODE_MASTER 1
#define M2TP_MODE_SLAVE  2

static const value_string m2tp_mode_values[] = {
  { M2TP_MODE_MASTER,      "Master" },
  { M2TP_MODE_SLAVE,       "Slave" },
  { 0, NULL}};

#define M2TP_ERROR_CODE_INVALID_VERSION                         1
#define M2TP_ERROR_CODE_INVALID_INTERFACE_IDENTIFIER            2
#define M2TP_ERROR_CODE_INVALID_ADAPTATION_LAYER_IDENTIFIER     3
#define M2TP_ERROR_CODE_INVALID_MESSAGE_TYPE                    4
#define M2TP_ERROR_CODE_INVALID_TRAFFIC_HANDLING_MODE           5
#define M2TP_ERROR_CODE_UNEXPECTED_MESSAGE                      6
#define M2TP_ERROR_CODE_PROTOCOL_ERROR                          7
#define M2TP_ERROR_CODE_INVALID_STREAM_IDENTIFIER               8
#define M2TP_ERROR_CODE_INCOMPATIBLE_MASTER_SLAVE_CONFIGURATION 9

static const value_string m2tp_error_code_values[] = {
  { M2TP_ERROR_CODE_INVALID_VERSION,                        "Invalid Version" },
  { M2TP_ERROR_CODE_INVALID_INTERFACE_IDENTIFIER,           "Invalid Interface Identifier" },
  { M2TP_ERROR_CODE_INVALID_ADAPTATION_LAYER_IDENTIFIER,    "Invalid Adaptation Layer Identifier" },
  { M2TP_ERROR_CODE_INVALID_MESSAGE_TYPE,                   "Invalid Message Type" },
  { M2TP_ERROR_CODE_INVALID_TRAFFIC_HANDLING_MODE,          "Invalid Traffic Handling Mode" },
  { M2TP_ERROR_CODE_UNEXPECTED_MESSAGE,                     "Unexpected Message" },
  { M2TP_ERROR_CODE_PROTOCOL_ERROR,                         "Protocol Error" },
  { M2TP_ERROR_CODE_INVALID_STREAM_IDENTIFIER,              "Invalid Stream Identified" },
  { M2TP_ERROR_CODE_INCOMPATIBLE_MASTER_SLAVE_CONFIGURATION,"Incompatible Master Slave Configuration" },
  { 0,                                                      NULL } };

#define MANAGEMENT_ORDER_REASON_CODE       1
#define MTP_RELEASE_REASON_CODE            2

static const value_string m2tp_reason_code_values[] = {
  { MANAGEMENT_ORDER_REASON_CODE,                      "Management Order" },
  { MTP_RELEASE_REASON_CODE,                           "MTP Release" },
  { 0,                                                 NULL } };


/* Initialize the protocol and registered fields */
static int proto_m2tp;
static int hf_m2tp_version;
static int hf_m2tp_reserved;
static int hf_m2tp_message_class;
static int hf_m2tp_message_type;
static int hf_m2tp_message_length;
static int hf_m2tp_parameter_tag;
static int hf_m2tp_parameter_length;
static int hf_m2tp_parameter_value;
static int hf_m2tp_parameter_padding;
static int hf_m2tp_interface_identifier;
static int hf_m2tp_user;
static int hf_m2tp_master_slave;
static int hf_m2tp_info_string;
static int hf_m2tp_heartbeat_data;
static int hf_m2tp_diagnostic_info;
static int hf_m2tp_error_code;
static int hf_m2tp_reason;

/* Initialize the subtree pointers */
static int ett_m2tp;
static int ett_m2tp_parameter;

static dissector_handle_t mtp2_handle;

static unsigned
nr_of_padding_bytes (unsigned length)
{
  unsigned remainder;

  remainder = length % 4;

  if (remainder == 0)
    return 0;
  else
    return 4 - remainder;
}

/* Common Header */
static void
dissect_m2tp_common_header(tvbuff_t *common_header_tvb, packet_info *pinfo, proto_tree *m2tp_tree)
{
  uint8_t version, reserved, message_class, message_type;
  uint32_t message_length;

  /* Extract the common header */
  version        = tvb_get_uint8(common_header_tvb, VERSION_OFFSET);
  reserved       = tvb_get_uint8(common_header_tvb, RESERVED_OFFSET);
  message_class  = tvb_get_uint8(common_header_tvb, MESSAGE_CLASS_OFFSET);
  message_type   = tvb_get_uint8(common_header_tvb, MESSAGE_TYPE_OFFSET);
  message_length = tvb_get_ntohl (common_header_tvb, MESSAGE_LENGTH_OFFSET);

  col_add_fstr(pinfo->cinfo, COL_INFO, "%s ", val_to_str_const(message_class * 256 + message_type, m2tp_message_class_type_acro_values, "reserved"));

  if (m2tp_tree) {
    /* add the components of the common header to the protocol tree */
    proto_tree_add_uint(m2tp_tree, hf_m2tp_version, common_header_tvb, VERSION_OFFSET, VERSION_LENGTH, version);
    proto_tree_add_uint(m2tp_tree, hf_m2tp_reserved, common_header_tvb, RESERVED_OFFSET, RESERVED_LENGTH, reserved);
    proto_tree_add_uint(m2tp_tree, hf_m2tp_message_class, common_header_tvb, MESSAGE_CLASS_OFFSET, MESSAGE_CLASS_LENGTH, message_class);
    proto_tree_add_uint_format_value(m2tp_tree, hf_m2tp_message_type,
                               common_header_tvb, MESSAGE_TYPE_OFFSET, MESSAGE_TYPE_LENGTH,
                               message_type, "%u (%s)",
                               message_type, val_to_str_const(message_class * 256 + message_type, m2tp_message_class_type_values, "reserved"));
    proto_tree_add_uint(m2tp_tree, hf_m2tp_message_length, common_header_tvb, MESSAGE_LENGTH_OFFSET, MESSAGE_LENGTH_LENGTH, message_length);
  };
}

/* Interface Identifier */
static void
dissect_m2tp_interface_identifier_parameter(tvbuff_t *parameter_tvb, packet_info *pinfo _U_, proto_tree *parameter_tree, proto_item *parameter_item)
{
  uint32_t parameter_value;

  if (parameter_tree) {
    parameter_value = tvb_get_ntohl(parameter_tvb, PARAMETER_VALUE_OFFSET);
    proto_tree_add_uint(parameter_tree, hf_m2tp_interface_identifier, parameter_tvb, INTERFACE_IDENTIFIER_OFFSET, INTERFACE_IDENTIFIER_LENGTH, parameter_value);
    proto_item_set_text(parameter_item, "Interface Identifier (%u)", parameter_value);
  }
}

/* Master Slave Indicator */
static void
dissect_m2tp_master_slave_parameter (tvbuff_t *parameter_tvb, packet_info *pinfo _U_, proto_tree *parameter_tree, proto_item *parameter_item)
{
  uint32_t parameter_value;

  if (parameter_tree) {
    parameter_value = tvb_get_ntohl(parameter_tvb, PARAMETER_VALUE_OFFSET);
    proto_tree_add_uint(parameter_tree, hf_m2tp_master_slave, parameter_tvb, MASTER_SLAVE_OFFSET, MASTER_SLAVE_LENGTH, parameter_value);
    proto_item_set_text(parameter_item, "Master Slave Indicator (%s)", val_to_str_const(parameter_value, m2tp_mode_values, "unknown"));
  }
}

/* M2tp User Identifier */
static void
dissect_m2tp_user_identifier_parameter (tvbuff_t *parameter_tvb, packet_info *pinfo _U_, proto_tree *parameter_tree, proto_item *parameter_item)
{
  uint32_t parameter_value;

  if (parameter_tree) {
    parameter_value = tvb_get_ntohl(parameter_tvb, PARAMETER_VALUE_OFFSET);
    proto_tree_add_uint(parameter_tree, hf_m2tp_user, parameter_tvb, M2TP_USER_OFFSET, M2TP_USER_LENGTH, parameter_value);
    proto_item_set_text(parameter_item, "M2TP User Identifier (%u)", parameter_value);
  }
}

/* Info String */
static void
dissect_m2tp_info_parameter(tvbuff_t *parameter_tvb, packet_info *pinfo, proto_tree *parameter_tree, proto_item *parameter_item)
{
  uint16_t length, info_string_length;
  const uint8_t *info_string;

  if (parameter_tree) {
    length = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
    info_string_length = length - PARAMETER_HEADER_LENGTH;
    proto_tree_add_item_ret_string(parameter_tree, hf_m2tp_info_string, parameter_tvb, INFO_STRING_OFFSET, info_string_length, ENC_ASCII, pinfo->pool, &info_string);
    proto_item_set_text(parameter_item, "Info String (%s)", info_string);
  }
}

/* Diagnostic Information */
static void
dissect_m2tp_diagnostic_information_parameter(tvbuff_t *parameter_tvb, packet_info *pinfo _U_, proto_tree *parameter_tree, proto_item *parameter_item)
{
  uint16_t length, diagnostic_info_length;

  if (parameter_tree) {
    length = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
    diagnostic_info_length = length - PARAMETER_HEADER_LENGTH;
    proto_tree_add_item(parameter_tree, hf_m2tp_diagnostic_info, parameter_tvb, PARAMETER_VALUE_OFFSET, diagnostic_info_length, ENC_NA);
    proto_item_set_text(parameter_item, "Diagnostic information (%u byte%s)", diagnostic_info_length, plurality(diagnostic_info_length, "", "s"));
  }
}

/* Heartbeat Data */
static void
dissect_m2tp_heartbeat_data_parameter(tvbuff_t *parameter_tvb, packet_info *pinfo _U_, proto_tree *parameter_tree, proto_item *parameter_item)
{
  uint16_t length, heartbeat_data_length;

  if (parameter_tree) {
    length = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
    heartbeat_data_length = length - PARAMETER_HEADER_LENGTH;
    proto_tree_add_item(parameter_tree, hf_m2tp_heartbeat_data, parameter_tvb, PARAMETER_VALUE_OFFSET, heartbeat_data_length, ENC_NA);
    proto_item_set_text(parameter_item, "Heartbeat data (%u byte%s)", heartbeat_data_length, plurality(heartbeat_data_length, "", "s"));
  }
}

/* Reason Parameter */
static void
dissect_m2tp_reason_parameter(tvbuff_t *parameter_tvb, packet_info *pinfo _U_, proto_tree *parameter_tree, proto_item *parameter_item)
{
  uint32_t reason;

  if (parameter_tree) {
    reason = tvb_get_ntohl(parameter_tvb, REASON_OFFSET);
    proto_tree_add_uint(parameter_tree, hf_m2tp_reason, parameter_tvb, REASON_OFFSET, REASON_LENGTH, reason);
    proto_item_set_text(parameter_item, "Reason parameter (%s)", val_to_str_const(reason, m2tp_reason_code_values, "unknown"));
  }
}

/* Error Code */
static void
dissect_m2tp_error_code_parameter(tvbuff_t *parameter_tvb, packet_info *pinfo _U_, proto_tree *parameter_tree, proto_item *parameter_item)
{
  uint32_t error_code;

  if (parameter_tree) {
    error_code = tvb_get_ntohl(parameter_tvb, ERROR_CODE_OFFSET);
    proto_tree_add_uint(parameter_tree, hf_m2tp_error_code, parameter_tvb, ERROR_CODE_OFFSET, ERROR_CODE_LENGTH, error_code);
    proto_item_set_text(parameter_item, "Error code parameter (%s)", val_to_str_const(error_code, m2tp_error_code_values, "unknown"));
  }
}

/* Protocol Data */
static void
dissect_m2tp_protocol_data_parameter(tvbuff_t *parameter_tvb, packet_info *pinfo, proto_tree *parameter_tree, proto_item *parameter_item, proto_item *m2tp_item, proto_tree *tree)
{
  uint16_t length, protocol_data_length, padding_length;
  tvbuff_t *mtp2_tvb;

  length               = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
  padding_length       = nr_of_padding_bytes(length);
  protocol_data_length = length - PARAMETER_HEADER_LENGTH;

  mtp2_tvb = tvb_new_subset_length(parameter_tvb, PARAMETER_VALUE_OFFSET, protocol_data_length);
  call_dissector(mtp2_handle, mtp2_tvb, pinfo, tree);

  if (parameter_tree) {
    proto_item_set_text(parameter_item, "Protocol data (SS7 message)");
    proto_item_set_len(parameter_item, proto_item_get_len(parameter_item) - protocol_data_length - padding_length);
    proto_item_set_len(m2tp_item,      proto_item_get_len(m2tp_item)      - protocol_data_length - padding_length);

  }
}

/* Unknown Parameter */
static void
dissect_m2tp_unknown_parameter(tvbuff_t *parameter_tvb, packet_info *pinfo _U_, proto_tree *parameter_tree, proto_item *parameter_item)
{
  uint16_t tag, length, parameter_value_length;

  if (parameter_tree) {
    tag    = tvb_get_ntohs(parameter_tvb, PARAMETER_TAG_OFFSET);
    length = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);

    parameter_value_length = length - PARAMETER_HEADER_LENGTH;
    proto_tree_add_item(parameter_tree, hf_m2tp_parameter_value, parameter_tvb, PARAMETER_VALUE_OFFSET, parameter_value_length, ENC_NA);

    proto_item_set_text(parameter_item, "Parameter with tag %u and %u byte%s value", tag, parameter_value_length, plurality(parameter_value_length, "", "s"));
  }
}

/* M2TP Parameter */
static void
dissect_m2tp_parameter(tvbuff_t *parameter_tvb, packet_info *pinfo, proto_tree *m2tp_tree, proto_item *m2tp_item, proto_tree *tree)
{
  uint16_t tag, length, padding_length, total_length;
  proto_item *parameter_item = NULL;
  proto_tree *parameter_tree = NULL;

  /* extract tag and length from the parameter */
  tag            = tvb_get_ntohs(parameter_tvb, PARAMETER_TAG_OFFSET);
  length         = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);

  /* calculate padding and total length */
  padding_length = nr_of_padding_bytes(length);
  total_length   = length + padding_length;

  if (tree) {
    /* create proto_tree stuff */
    parameter_tree = proto_tree_add_subtree(m2tp_tree, parameter_tvb, PARAMETER_HEADER_OFFSET, total_length,
                                        ett_m2tp_parameter, &parameter_item, "Incomplete parameter");

    /* add tag and length to the m2tp tree */
    proto_tree_add_uint(parameter_tree, hf_m2tp_parameter_tag, parameter_tvb, PARAMETER_TAG_OFFSET, PARAMETER_TAG_LENGTH, tag);
    proto_tree_add_uint(parameter_tree, hf_m2tp_parameter_length, parameter_tvb, PARAMETER_LENGTH_OFFSET, PARAMETER_LENGTH_LENGTH, length);
  }

  switch(tag) {
    case INTERFACE_IDENTIFIER_PARAMETER_TAG:
      dissect_m2tp_interface_identifier_parameter(parameter_tvb, pinfo, parameter_tree, parameter_item);
      break;
    case MASTER_SLAVE_INDICATOR_PARAMETER_TAG:
      dissect_m2tp_master_slave_parameter(parameter_tvb, pinfo, parameter_tree, parameter_item);
      break;
    case M2TP_USER_IDENTIFIER_PARAMETER_TAG:
      dissect_m2tp_user_identifier_parameter(parameter_tvb, pinfo, parameter_tree, parameter_item);
      break;
    case INFO_PARAMETER_TAG:
      dissect_m2tp_info_parameter(parameter_tvb, pinfo, parameter_tree, parameter_item);
      break;
    case DIAGNOSTIC_INFORMATION_PARAMETER_TAG:
      dissect_m2tp_diagnostic_information_parameter(parameter_tvb, pinfo, parameter_tree, parameter_item);
      break;
    case HEARTBEAT_DATA_PARAMETER_TAG:
      dissect_m2tp_heartbeat_data_parameter(parameter_tvb, pinfo, parameter_tree, parameter_item);
      break;
    case REASON_PARAMETER_TAG:
      dissect_m2tp_reason_parameter(parameter_tvb, pinfo, parameter_tree, parameter_item);
      break;
    case ERROR_CODE_PARAMETER_TAG:
      dissect_m2tp_error_code_parameter(parameter_tvb, pinfo, parameter_tree, parameter_item);
      break;
    case PROTOCOL_DATA_PARAMETER_TAG:
      dissect_m2tp_protocol_data_parameter(parameter_tvb, pinfo, parameter_tree, parameter_item, m2tp_item, tree);
      break;
    default:
      dissect_m2tp_unknown_parameter(parameter_tvb, pinfo, parameter_tree, parameter_item);
      break;
  };

  if ((parameter_tree) && (padding_length > 0))
    proto_tree_add_item(parameter_tree, hf_m2tp_parameter_padding, parameter_tvb, PARAMETER_HEADER_OFFSET + length, padding_length, ENC_NA);
}

/* M2TP Message */
static void
dissect_m2tp_message(tvbuff_t *message_tvb, packet_info *pinfo, proto_item *m2tp_item, proto_tree *m2tp_tree, proto_tree *tree)
{
  int offset, length, padding_length, total_length;
  tvbuff_t *common_header_tvb, *parameter_tvb;

  offset = 0;

  /* extract and process the common header */
  common_header_tvb = tvb_new_subset_length(message_tvb, offset, COMMON_HEADER_LENGTH);
  dissect_m2tp_common_header(common_header_tvb, pinfo, m2tp_tree);
  offset += COMMON_HEADER_LENGTH;

  /* extract zero or more parameters and process them individually */
  while(tvb_reported_length_remaining(message_tvb, offset) > 0) {
    length         = tvb_get_ntohs(message_tvb, offset + PARAMETER_LENGTH_OFFSET);
    padding_length = nr_of_padding_bytes(length);
    total_length   = length + padding_length;
    /* create a tvb for the parameter including the padding bytes */
    parameter_tvb    = tvb_new_subset_length(message_tvb, offset, total_length);
    dissect_m2tp_parameter(parameter_tvb, pinfo, m2tp_tree, m2tp_item, tree);
    /* get rid of the handled parameter */
    offset += total_length;
  }
}

/* M2tp */
static int
dissect_m2tp(tvbuff_t *message_tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
  proto_item *m2tp_item;
  proto_tree *m2tp_tree;

  /* make entry in the Protocol column on summary display */
  col_set_str(pinfo->cinfo, COL_PROTOCOL, "M2TP");

  /* create the m2tp protocol tree */
  m2tp_item = proto_tree_add_item(tree, proto_m2tp, message_tvb, 0, -1, ENC_NA);
  m2tp_tree = proto_item_add_subtree(m2tp_item, ett_m2tp);

  /* dissect the message */
  dissect_m2tp_message(message_tvb, pinfo, m2tp_item, m2tp_tree, tree);
  return tvb_captured_length(message_tvb);
}

/* Register the protocol with Wireshark */
void
proto_register_m2tp(void)
{

  /* Setup list of header fields */
  static hf_register_info hf[] = {
    { &hf_m2tp_version,
      { "Version", "m2tp.version",
        FT_UINT8, BASE_DEC, VALS(m2tp_protocol_version_values), 0x0,
        NULL, HFILL}
    },
    { &hf_m2tp_reserved,
      { "Reserved", "m2tp.reserved",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL}
    },
    { &hf_m2tp_message_class,
      { "Message class", "m2tp.message_class",
        FT_UINT8, BASE_DEC, VALS(m2tp_message_class_values), 0x0,
        NULL, HFILL}
    },
    { &hf_m2tp_message_type,
      { "Message Type", "m2tp.message_type",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL}
    },
    { &hf_m2tp_message_length,
      { "Message length", "m2tp.message_length",
        FT_UINT32, BASE_DEC, NULL, 0x0,
        NULL, HFILL}
    },
    { &hf_m2tp_parameter_tag,
      { "Parameter Tag", "m2tp.parameter_tag",
        FT_UINT16, BASE_DEC, VALS(m2tp_parameter_tag_values), 0x0,
        NULL, HFILL}
    },
    { &hf_m2tp_parameter_length,
      { "Parameter length", "m2tp.parameter_length",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        NULL, HFILL}
    },
    { &hf_m2tp_parameter_value,
      { "Parameter Value", "m2tp.parameter_value",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_m2tp_parameter_padding,
      { "Padding", "m2tp.parameter_padding",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_m2tp_interface_identifier,
      { "Interface Identifier", "m2tp.interface_identifier",
        FT_UINT32, BASE_DEC, NULL, 0x0,
        NULL, HFILL}
    },
    { &hf_m2tp_user,
      { "M2tp User Identifier", "m2tp.user_identifier",
        FT_UINT32, BASE_DEC, VALS(m2tp_user_identifier_values), 0x0,
        NULL, HFILL}
    },
    { &hf_m2tp_master_slave,
      { "Master Slave Indicator", "m2tp.master_slave",
        FT_UINT32, BASE_DEC, VALS(m2tp_mode_values), 0x0,
        NULL, HFILL}
    },
    { &hf_m2tp_info_string,
      { "Info string", "m2tp.info_string",
        FT_STRING, BASE_NONE, NULL, 0x0,
        NULL, HFILL}
    },
    { &hf_m2tp_diagnostic_info,
      { "Diagnostic information", "m2tp.diagnostic_info",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_m2tp_heartbeat_data,
      { "Heartbeat data", "m2tp.heartbeat_data",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_m2tp_error_code,
      { "Error code", "m2tp.error_code",
        FT_UINT32, BASE_DEC, VALS(m2tp_error_code_values), 0x0,
        NULL, HFILL}
    },
    { &hf_m2tp_reason,
      { "Reason", "m2tp.reason",
        FT_UINT32, BASE_DEC, NULL, 0x0,
        NULL, HFILL}
    },
  };

  /* Setup protocol subtree array */
  static int *ett[] = {
    &ett_m2tp,
    &ett_m2tp_parameter,
  };

  /* Register the protocol name and description */
  proto_m2tp = proto_register_protocol("MTP 2 Transparent Proxy", "M2TP",  "m2tp");

  /* Required function calls to register the header fields and subtrees used */
  proto_register_field_array(proto_m2tp, hf, array_length(hf));
  proto_register_subtree_array(ett, array_length(ett));

  /* Register the dissector */
  m2tp_handle = register_dissector("m2tp", dissect_m2tp, proto_m2tp);
}

void
proto_reg_handoff_m2tp(void)
{
  mtp2_handle = find_dissector_add_dependency("mtp2", proto_m2tp);
  dissector_add_uint("sctp.ppi",  M2TP_PAYLOAD_PROTOCOL_ID, m2tp_handle);
  dissector_add_uint("sctp.port", SCTP_PORT_M2TP, m2tp_handle);
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local Variables:
 * c-basic-offset: 2
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=2 tabstop=8 expandtab:
 * :indentSize=2:tabSize=8:noTabs=true:
 */