Document the new Copy Profile button.
[obnox/wireshark/wip.git] / epan / dissectors / packet-jmirror.c
1 /* packet-jmirror.c
2  * Routines for Jmirror protocol packet disassembly
3  * By Wayne Brassem <wbrassem@juniper.net>
4  * Copyright 2009 Wayne Brassem
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <epan/packet.h>
32 #include <epan/prefs.h>
33
34 #define MIRROR_HDR_SZ           8
35 #define MIRROR_ID_SZ            4
36 #define SESSION_ID_SZ           4
37 #define DEF_JMIRROR_UDP_PORT    30030 /* a product of primes (1*2*3*5*7*11*13)  :-) */
38
39 /*
40  * See www.juniper.net JUNOSe Packet Mirroring documentation
41  */
42
43 /* Jmirror protocol variables */
44 static int proto_jmirror = -1;
45 static int hf_jmirror_mid = -1;
46 static int hf_jmirror_sid = -1;
47 static gint ett_jmirror = -1;
48
49 /* Handles which point to the packet dissectors */
50 static dissector_handle_t ipv4_handle;
51 static dissector_handle_t ipv6_handle;
52 static dissector_handle_t hdlc_handle;
53
54 static guint global_jmirror_udp_port = DEF_JMIRROR_UDP_PORT;
55
56 /* Forward declaration */
57 void proto_reg_handoff_jmirror(void);
58
59 /* Routine to return the dissector handle based on heuristic packet inspection */
60 static dissector_handle_t
61 get_heuristic_handle(tvbuff_t *tvb)
62 {
63         int offset = MIRROR_HDR_SZ;            /* Point past the 8 byte mirror header */
64         int byte0, byte1, byte2, byte3;
65
66         /* The following section is designed to determine the nature of the mirrored packet.
67          *
68          * The first four bytes will be inspected to deduce the type of traffic.
69          * The bit pattern shown below is the basis.  A bit of "x" is a variable field.
70          *
71          * IPv4 Header: 0100 0101 xxxx xx00                     ==> Pattern for standard IPv4 20-byte header
72          * IPv6 Header: 0110 xxxx xxxx 0000 0000 0000 0000 0000 ==> Pattern for standard IPv6 header with no flow label
73          * PPP/HDLC:    1111 1111 0000 0011 xx00 0000 0010 0001 ==> HDLC-like framing for PPP (FF 03 x0 21)
74          */
75
76         if (!tvb_bytes_exist(tvb, offset, 4))
77                 return NULL;   /* Not enough bytes for heuristic test */
78
79         /* Filter for IPv4 and IPv6 packets */
80         byte0 = tvb_get_guint8(tvb, offset + 0);
81         byte1 = tvb_get_guint8(tvb, offset + 1);
82         byte2 = tvb_get_guint8(tvb, offset + 2);
83         byte3 = tvb_get_guint8(tvb, offset + 3);
84
85         /* Look for IPv4 with standard header length */
86         if ( byte0 == 0x45 && ipv4_handle )
87                 return ipv4_handle;
88
89         /* Look for IPv6 */
90         else if ( hi_nibble(byte0) == 6 && lo_nibble(byte1) == 0 && byte2 == 0 && byte3 == 0 && ipv6_handle )
91                 return ipv6_handle;
92
93         /* Look for PPP/HDLC */
94         else if ( byte0 == 0xff && byte1 == 0x03 && lo_nibble(byte2) == 0 && byte3 == 0x21 && hdlc_handle )
95                 return hdlc_handle;
96
97         /* If it's something else entirely return nothing */
98         else
99                 return NULL;
100 }
101
102 /* Heuristic UDP dissector called by Wireshark looking for embedded IPv4, IPv6 or L2TP/HDLC Jmirror packets */
103 static int
104 dissect_jmirror(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
105 {
106         int offset = 0;
107         dissector_handle_t dissector_handle;
108         unsigned int midval, sidval;
109         proto_item *ti = NULL;
110         proto_tree *jmirror_tree = NULL;
111         tvbuff_t *next_tvb = NULL;
112
113         if ( !( dissector_handle = get_heuristic_handle(tvb) ) )
114                 return 0;
115
116         /* Populate the Protocol field in the Wireshark packet display */
117         col_set_str(pinfo->cinfo, COL_PROTOCOL, "Jmirror");
118
119         /* Build the Jmirror Identifier value and store the string in a buffer */
120         midval = tvb_get_ntohl(tvb, offset);
121
122         /* Build the Session Identifier value and store the string in a buffer */
123         sidval = tvb_get_ntohl(tvb, offset+MIRROR_ID_SZ);
124
125         /* Populate the Info field in the Wireshark packet display */
126         col_add_fstr(pinfo->cinfo, COL_INFO, "MID: 0X%08x (%d), SID: 0x%08x (%d)", midval, midval, sidval, sidval);
127
128         /* Create a header for the Mirror Header in the protocol tree */
129         ti = proto_tree_add_protocol_format(tree, proto_jmirror, tvb, offset, MIRROR_HDR_SZ,
130                 "Juniper Packet Mirror, MID: 0x%08x (%d), SID: 0x%08x (%d)", midval, midval, sidval, sidval);
131
132         /* Add the Juniper Packet Mirror to the main protocol tree */
133         jmirror_tree = proto_item_add_subtree(ti, ett_jmirror);
134
135         /* Insert the Jmirror Identifier into the protocol tree and assign value to filter variable */
136         proto_tree_add_item(jmirror_tree, hf_jmirror_mid, tvb, offset, MIRROR_ID_SZ, FALSE);
137
138         /* Push the tvbuff_t offset pointer along to the Session Identifier */
139         offset += MIRROR_ID_SZ;
140
141         /* Insert the Session Identifier into the protocol tree and assign value to filter variable */
142         proto_tree_add_item(jmirror_tree, hf_jmirror_sid, tvb, offset, SESSION_ID_SZ, FALSE);
143
144         /* Push the tvbuff_t offset pointer along to the start of the mirrored packet */
145         offset += SESSION_ID_SZ;
146
147         /* Create a buffer pointer for the next dissector */
148         next_tvb = tvb_new_subset(tvb, offset, -1, -1);
149
150         /* Call the next dissector based on the heurstics and return the number of bytes dissected */
151         return MIRROR_HDR_SZ + call_dissector(dissector_handle, next_tvb, pinfo, tree);
152
153 }
154
155 /* Register Jmirror dissector with Wireshark */
156 void
157 proto_register_jmirror(void)
158 {
159         module_t        *jmirror_module = NULL;
160
161         /* Used by the Expression dialog and filter box */
162         static hf_register_info jmirror_hf[] = {
163                 { &hf_jmirror_mid,
164                   { "Jmirror Identifier", "jmirror.mid", FT_UINT32, BASE_HEX_DEC, NULL, 0x0,
165                     "Unique identifier of the mirrored session", HFILL }
166                 },
167                 { &hf_jmirror_sid,
168                   { "Session Identifier", "jmirror.sid", FT_UINT32, BASE_HEX_DEC, NULL, 0x0,
169                     "Unique identifier of the user session", HFILL }
170                 }
171         };
172         static gint *jmirror_ett[] = {
173                 &ett_jmirror
174         };
175
176         /* Register the Jmirror protocol with Wireshark */
177         proto_jmirror = proto_register_protocol("Juniper Packet Mirror", "Jmirror", "jmirror");
178
179         /* Register the Jmirror preferences with Wireshark */
180         jmirror_module = prefs_register_protocol(proto_jmirror, proto_reg_handoff_jmirror);
181
182         /* Allow the user to set the UDP port for the decode under the Edit -> Preferences menu */
183         prefs_register_uint_preference(jmirror_module, "udp.port", "JMirror UDP Port",
184                 "Set the port for JMirror Port (if other than the default of 30030)",
185                 10, &global_jmirror_udp_port);
186
187         /* Register the Jmirror subfields for filters */
188         proto_register_field_array(proto_jmirror, jmirror_hf, array_length(jmirror_hf));
189         proto_register_subtree_array(jmirror_ett, array_length(jmirror_ett));
190 }
191
192 /* Create attachment point for dissector in Wireshark */
193 void
194 proto_reg_handoff_jmirror(void)
195 {
196         static int jmirror_inited = FALSE;
197         static guint jmirror_udp_port;
198         static dissector_handle_t jmirror_handle;
199
200         if ( !jmirror_inited )
201         {
202                 /* register as heuristic dissector for UDP */
203                 /* heur_dissector_add("udp", dissect_jmirror, proto_jmirror); */
204
205                 /* Create a dissector handle for the Jmirror protocol */
206                 jmirror_handle = new_create_dissector_handle(dissect_jmirror, proto_jmirror);
207
208                 /* Create pointer to ipv4, ipv6, ppp and data dissectors */
209                 ipv4_handle = find_dissector("ip");
210                 ipv6_handle = find_dissector("ipv6");
211                 hdlc_handle = find_dissector("pw_hdlc_nocw_hdlc_ppp");
212
213                 /* Set the init flag */
214                 jmirror_inited = TRUE;
215         } else {
216                 /* Unregister from the old UDP port */
217                 dissector_delete("udp.port", jmirror_udp_port, jmirror_handle);
218         }
219
220         jmirror_udp_port = global_jmirror_udp_port;
221
222         /* Register as a normal IP dissector with default UDP port 30030 */
223         dissector_add("udp.port", jmirror_udp_port, jmirror_handle);
224 }