aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-loop.c
blob: 68c2f0029a9ab90895212e3370ecad2c65a81d7b (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
/* packet-loop.c
 * Routines for Ethernet loopback/Configuration Test Protocol dissection,
 * as documented in section 8 "Ethernet Configuration Testing Protocol" of
 * the v2.0 DIX Ethernet specification.
 *
 * See
 *
 *    http://decnet.ipv7.net/docs/dundas/aa-k759b-tk.pdf
 *
 * for a copy of the DIX spec and
 *
 *    http://stuff.mit.edu/people/jhawk/ctp.html
 *
 * for section 8.
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/etypes.h>

void proto_register_loop(void);
void proto_reg_handoff_loop(void);

static int proto_loop = -1;
static int hf_loop_skipcount = -1;
static int hf_loop_function = -1;
static int hf_loop_relevant_function = -1;
static int hf_loop_receipt_number = -1;
static int hf_loop_forwarding_address = -1;

static gint ett_loop = -1;

#define FUNC_REPLY              1
#define FUNC_FORWARD_DATA       2

static const value_string function_vals[] = {
  { FUNC_REPLY, "Reply" },
  { FUNC_FORWARD_DATA, "Forward Data" },
  { 0, NULL }
};

static int
dissect_loop(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
  proto_tree  *loop_tree = NULL;
  proto_item  *ti;
  guint16     function;
  int         offset = 0;
  int         skip_offset;
  gboolean    set_info = TRUE;
  gboolean    more_function;
  tvbuff_t    *next_tvb;

  col_set_str(pinfo->cinfo, COL_PROTOCOL, "LOOP");
  col_clear(pinfo->cinfo, COL_INFO);

  if (tree) {
    ti = proto_tree_add_item(tree, proto_loop, tvb, offset, -1, ENC_NA);
    loop_tree = proto_item_add_subtree(ti, ett_loop);

    proto_tree_add_item(loop_tree, hf_loop_skipcount, tvb, offset, 2, ENC_LITTLE_ENDIAN);
  }
  skip_offset = 2 + tvb_get_letohs(tvb, offset);
  offset += 2;

  do {
    function = tvb_get_letohs(tvb, offset);
    if (offset == skip_offset) {
      col_add_str(pinfo->cinfo, COL_INFO,
                    val_to_str(function, function_vals, "Unknown function (%u)"));

      proto_tree_add_uint(loop_tree, hf_loop_relevant_function, tvb, offset, 2, function);
      set_info = FALSE;
    }
    proto_tree_add_uint(loop_tree, hf_loop_function, tvb, offset, 2, function);
    offset += 2;
    switch (function) {

    case FUNC_REPLY:
      proto_tree_add_item(loop_tree, hf_loop_receipt_number, tvb, offset, 2,
                            ENC_LITTLE_ENDIAN);
      offset += 2;
      more_function = FALSE;
      break;

    case FUNC_FORWARD_DATA:
      proto_tree_add_item(loop_tree, hf_loop_forwarding_address, tvb, offset,
                            6, ENC_NA);
      offset += 6;
      more_function = TRUE;
      break;

    default:
      more_function = FALSE;
      break;
    }
  } while (more_function);

  if (set_info) {
    col_set_str(pinfo->cinfo, COL_INFO, "No valid function found");
  }

  if (tvb_reported_length_remaining(tvb, offset) > 0)
  {
    next_tvb = tvb_new_subset_remaining(tvb, offset);
    call_data_dissector(next_tvb, pinfo, tree);
  }
  return tvb_captured_length(tvb);
}

void
proto_register_loop(void)
{
  static hf_register_info hf[] = {
    { &hf_loop_skipcount,
      { "skipCount",            "loop.skipcount",
    FT_UINT16,  BASE_DEC,       NULL,   0x0,
      NULL, HFILL }},

    { &hf_loop_function,
      { "Function",             "loop.function",
    FT_UINT16,  BASE_DEC,       VALS(function_vals),    0x0,
      NULL, HFILL }},

    { &hf_loop_relevant_function,
      { "Relevant function",            "loop.relevant_function",
    FT_UINT16,  BASE_DEC,       VALS(function_vals),    0x0,
      NULL, HFILL }},

    { &hf_loop_receipt_number,
      { "Receipt number",       "loop.receipt_number",
    FT_UINT16,  BASE_DEC,       NULL,   0x0,
      NULL, HFILL }},

    { &hf_loop_forwarding_address,
      { "Forwarding address",   "loop.forwarding_address",
    FT_ETHER,   BASE_NONE,      NULL,   0x0,
      NULL, HFILL }},
  };
  static gint *ett[] = {
    &ett_loop,
  };

  proto_loop = proto_register_protocol("Configuration Test Protocol (loopback)",
                                       "LOOP", "loop");
  proto_register_field_array(proto_loop, hf, array_length(hf));
  proto_register_subtree_array(ett, array_length(ett));
}

void
proto_reg_handoff_loop(void)
{
  dissector_handle_t loop_handle;

  loop_handle = create_dissector_handle(dissect_loop, proto_loop);

  dissector_add_uint("ethertype", ETHERTYPE_LOOP, loop_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:
 */