summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-rtag.c
blob: 2f7f3247c23f9c0bc1f0f78ca6ec067ae8e6fade (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
/* packet-rtag.c
 * Dissector for IEEE 802.1cb R-TAG tags
 * By Stephen Williams <steve.williams@getcruise.com>
 * Copyright 2020-present, Cruise LLC
 *
 * 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>

static int proto_rtag = -1;

static dissector_handle_t ethertype_handle;
static dissector_handle_t rtag_handle;

/*
 * These values and tables are a breakdown of the R-TAG parts.
 */
static int hf_rtag_reserved = -1;
static int hf_rtag_sequence = -1;
static int hf_rtag_protocol = -1;
static hf_register_info rtag_breakdown[] = {
      { &hf_rtag_reserved,
	{ "<reserved>", "rtag.reserved",
	  FT_UINT16, BASE_HEX,
	  NULL, 0x0,
	  NULL, HFILL }
      },
      { &hf_rtag_sequence,
	{ "Sequence number", "rtag.seqno",
	  FT_UINT16, BASE_DEC,
	  NULL, 0x0,
	  NULL, HFILL }
      },
      { &hf_rtag_protocol,
	{ "Type", "rtag.protocol",
	  FT_UINT16, BASE_HEX,
	  VALS(etype_vals), 0x0,
	  "Ethertype", HFILL }
      }
};

/*
 */
static gint ett_rtag = -1;
static gint *ett[] = { &ett_rtag };

/*
 * Dissect the R-TAG portion of a given packet. This is called with
 * the tvb pointing to where our payload starts (i.e. not including
 * the 0xf1c1 tag that got us here.)
 */
static int dissect_rtag(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
{
      proto_item*ti;
      col_set_str(pinfo->cinfo, COL_PROTOCOL, "R-TAG");
	/* Clear the info column */
      col_clear(pinfo->cinfo, COL_INFO);

	/*
	 * The R-TAG is 6 octets: 2 reserved, 2 sequence number, and
	 * 2 encapsulated protocol.
	 */
      ti = proto_tree_add_item(tree, proto_rtag, tvb, 0, 6, ENC_NA);

      guint16 seqno = tvb_get_ntohs(tvb, 2);
      guint16 rtag_protocol = tvb_get_ntohs(tvb, 4);

      proto_tree *rtag_subtree = proto_item_add_subtree(ti, ett_rtag);
      proto_tree_add_item(rtag_subtree, hf_rtag_reserved, tvb, 0, 2, ENC_BIG_ENDIAN);
      proto_tree_add_item(rtag_subtree, hf_rtag_sequence, tvb, 2, 2, ENC_BIG_ENDIAN);
      proto_tree_add_item(rtag_subtree, hf_rtag_protocol, tvb, 4, 2, ENC_BIG_ENDIAN);

	/* Add a quick summary in the info column. */
      col_add_fstr(pinfo->cinfo, COL_INFO, "R-TAG: %u", seqno);

	/*
	 * Process the encapsulated packet as an encapsulated Ethernet
	 * PDU. We have the encapsulated protocol type (and ethertype)
	 * as part of the R-TAG protocol
	 */
      ethertype_data_t ethertype_data;
      ethertype_data.etype = rtag_protocol;;
      ethertype_data.payload_offset = 6;
      ethertype_data.fh_tree = tree;
      ethertype_data.trailer_id = -1;
      ethertype_data.fcs_len = -1;
      call_dissector_with_data(ethertype_handle, tvb, pinfo, tree, &ethertype_data);

      return tvb_captured_length(tvb);
}

/*
 * This function is called to register a protocol description.
 */
void proto_register_rtag(void)
{
      proto_rtag = proto_register_protocol (
	   "802.1cb R-TAG", /* name        */
	   "R-TAG",         /* short name  */
	   "rtag"           /* filter_name */
	  );

      proto_register_field_array(proto_rtag, rtag_breakdown,
				 array_length(rtag_breakdown));
      proto_register_subtree_array(ett, array_length(ett));
}

/*
 * This function is called to register the actual dissector.
 */
void proto_reg_handoff_rtag(void)
{
      rtag_handle = create_dissector_handle(dissect_rtag, proto_rtag);
      dissector_add_uint("ethertype", ETHERTYPE_IEEE_802_1CB, rtag_handle);

	/* Get a handle for the ethertype dissector. */
      ethertype_handle = find_dissector_add_dependency("ethertype", proto_rtag);
}