Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / dissectors / packet-jmirror.c
blobdff8c5c077ce996fd41ec65a21b06d9e10932486
1 /* packet-jmirror.c
2 * Routines for Jmirror protocol packet disassembly
3 * By Wayne Brassem <wbrassem@juniper.net>
4 * Copyright 2009 Wayne Brassem
5 * 2012 Wayne Brassem - Correction to support IPv6 over PPP
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * SPDX-License-Identifier: GPL-2.0-or-later
14 #include "config.h"
16 #include <epan/packet.h>
18 void proto_register_jmirror(void);
19 void proto_reg_handoff_jmirror(void);
21 static dissector_handle_t jmirror_handle;
23 #define MIRROR_HDR_SZ 8
24 #define MIRROR_ID_SZ 4
25 #define SESSION_ID_SZ 4
26 #define DEF_JMIRROR_UDP_PORT 30030 /* a product of primes (1*2*3*5*7*11*13) :-) Not IANA registered */
29 * See www.juniper.net JUNOSe Packet Mirroring documentation
32 /* Jmirror protocol variables */
33 static int proto_jmirror;
34 static int hf_jmirror_mid;
35 static int hf_jmirror_sid;
36 static int ett_jmirror;
38 /* Handles which point to the packet dissectors */
39 static dissector_handle_t ipv4_handle;
40 static dissector_handle_t ipv6_handle;
41 static dissector_handle_t hdlc_handle;
43 /* Routine to return the dissector handle based on heuristic packet inspection */
44 static dissector_handle_t
45 get_heuristic_handle(tvbuff_t *tvb)
47 int offset = MIRROR_HDR_SZ; /* Point past the 8 byte mirror header */
48 int byte0, byte1, byte2, byte3;
50 /* The following section is designed to determine the nature of the mirrored packet.
52 * The first four bytes will be inspected to deduce the type of traffic.
53 * The bit pattern shown below is the basis. A bit of "x" is a variable field.
55 * IPv4 Header: 0100 0101 xxxx xx00 ==> Pattern for standard IPv4 20-byte header
56 * IPv6 Header: 0110 xxxx xxxx xxxx xxxx xxxx xxxx xxxx ==> Pattern for standard IPv6 header with variable TC and Flow
57 * PPP/HDLC: 1111 1111 0000 0011 xx00 0000 0010 0001 ==> HDLC-like framing for PPP (FF 03 x0 21)
58 * PPP/HDLC: 1111 1111 0000 0011 0000 0000 0101 0111 ==> HDLC-like framing for PPP IPv6 (FF 03 00 57)
61 if (!tvb_bytes_exist(tvb, offset, 4))
62 return NULL; /* Not enough bytes for heuristic test */
64 /* Filter for IPv4 and IPv6 packets */
65 byte0 = tvb_get_uint8(tvb, offset + 0);
66 byte1 = tvb_get_uint8(tvb, offset + 1);
67 byte2 = tvb_get_uint8(tvb, offset + 2);
68 byte3 = tvb_get_uint8(tvb, offset + 3);
70 /* Look for IPv4 with standard header length */
71 if ( byte0 == 0x45 && ipv4_handle )
72 return ipv4_handle;
74 /* Look for IPv6 */
75 else if ( hi_nibble(byte0) == 6 && ipv6_handle )
76 return ipv6_handle;
78 /* Look for PPP/HDLC for LCP and IPv4 */
79 else if ( byte0 == 0xff && byte1 == 0x03 && lo_nibble(byte2) == 0 && byte3 == 0x21 && hdlc_handle )
80 return hdlc_handle;
82 /* Look for PPP/HDLC for IPv6 */
83 else if ( byte0 == 0xff && byte1 == 0x03 && byte2 == 0 && byte3 == 0x57 && hdlc_handle )
84 return hdlc_handle;
86 /* If it's something else entirely return nothing */
87 else
88 return NULL;
91 /* Heuristic UDP dissector called by Wireshark looking for embedded IPv4, IPv6 or L2TP/HDLC Jmirror packets */
92 static int
93 dissect_jmirror(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
95 int offset = 0;
96 dissector_handle_t dissector_handle;
97 unsigned int midval, sidval;
98 proto_item *ti = NULL;
99 proto_tree *jmirror_tree = NULL;
100 tvbuff_t *next_tvb = NULL;
102 if ( !( dissector_handle = get_heuristic_handle(tvb) ) )
103 return 0;
105 /* Populate the Protocol field in the Wireshark packet display */
106 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Jmirror");
108 /* Build the Jmirror Identifier value and store the string in a buffer */
109 midval = tvb_get_ntohl(tvb, offset);
111 /* Build the Session Identifier value and store the string in a buffer */
112 sidval = tvb_get_ntohl(tvb, offset+MIRROR_ID_SZ);
114 /* Populate the Info field in the Wireshark packet display */
115 col_add_fstr(pinfo->cinfo, COL_INFO, "MID: 0X%08x (%d), SID: 0x%08x (%d)", midval, midval, sidval, sidval);
117 /* Create a header for the Mirror Header in the protocol tree */
118 ti = proto_tree_add_protocol_format(tree, proto_jmirror, tvb, offset, MIRROR_HDR_SZ,
119 "Juniper Packet Mirror, MID: 0x%08x (%d), SID: 0x%08x (%d)", midval, midval, sidval, sidval);
121 /* Add the Juniper Packet Mirror to the main protocol tree */
122 jmirror_tree = proto_item_add_subtree(ti, ett_jmirror);
124 /* Insert the Jmirror Identifier into the protocol tree and assign value to filter variable */
125 proto_tree_add_item(jmirror_tree, hf_jmirror_mid, tvb, offset, MIRROR_ID_SZ, ENC_BIG_ENDIAN);
127 /* Push the tvbuff_t offset pointer along to the Session Identifier */
128 offset += MIRROR_ID_SZ;
130 /* Insert the Session Identifier into the protocol tree and assign value to filter variable */
131 proto_tree_add_item(jmirror_tree, hf_jmirror_sid, tvb, offset, SESSION_ID_SZ, ENC_BIG_ENDIAN);
133 /* Push the tvbuff_t offset pointer along to the start of the mirrored packet */
134 offset += SESSION_ID_SZ;
136 /* Create a buffer pointer for the next dissector */
137 next_tvb = tvb_new_subset_remaining(tvb, offset);
139 /* Call the next dissector based on the heuristics and return the number of bytes dissected */
140 return MIRROR_HDR_SZ + call_dissector(dissector_handle, next_tvb, pinfo, tree);
144 /* Register Jmirror dissector with Wireshark */
145 void
146 proto_register_jmirror(void)
148 /* Used by the Expression dialog and filter box */
149 static hf_register_info jmirror_hf[] = {
150 { &hf_jmirror_mid,
151 { "Jmirror Identifier", "jmirror.mid", FT_UINT32, BASE_HEX_DEC, NULL, 0x0,
152 "Unique identifier of the mirrored session", HFILL }
154 { &hf_jmirror_sid,
155 { "Session Identifier", "jmirror.sid", FT_UINT32, BASE_HEX_DEC, NULL, 0x0,
156 "Unique identifier of the user session", HFILL }
159 static int *jmirror_ett[] = {
160 &ett_jmirror
163 /* Register the Jmirror protocol with Wireshark */
164 proto_jmirror = proto_register_protocol("Juniper Packet Mirror", "Jmirror", "jmirror");
166 /* Register the Jmirror subfields for filters */
167 proto_register_field_array(proto_jmirror, jmirror_hf, array_length(jmirror_hf));
168 proto_register_subtree_array(jmirror_ett, array_length(jmirror_ett));
170 /* Create a dissector handle for the Jmirror protocol */
171 jmirror_handle = register_dissector("jmirror", dissect_jmirror, proto_jmirror);
175 /* Create attachment point for dissector in Wireshark */
176 void
177 proto_reg_handoff_jmirror(void)
180 /* register as heuristic dissector for UDP */
181 /* heur_dissector_add("udp", dissect_jmirror, proto_jmirror); */
183 /* Create pointer to ipv4, ipv6, ppp and data dissectors */
184 ipv4_handle = find_dissector_add_dependency("ip", proto_jmirror);
185 ipv6_handle = find_dissector_add_dependency("ipv6", proto_jmirror);
186 hdlc_handle = find_dissector_add_dependency("pw_hdlc_nocw_hdlc_ppp", proto_jmirror);
188 /* Register as a normal IP dissector with default UDP port 30030 */
189 dissector_add_uint_with_preference("udp.port", DEF_JMIRROR_UDP_PORT, jmirror_handle);
193 * Editor modelines - https://www.wireshark.org/tools/modelines.html
195 * Local variables:
196 * c-basic-offset: 8
197 * tab-width: 8
198 * indent-tabs-mode: t
199 * End:
201 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
202 * :indentSize=8:tabSize=8:noTabs=false: