Cleanup use of #includes in non-generated epan/dissector/*.c
[metze/wireshark/wip.git] / epan / dissectors / packet-quic.c
1 /* packet-quic.c
2  * Routines for Quick UDP Internet Connections dissection
3  * Copyright 2013, Alexis La Goutte <alexis.lagoutte at gmail dot com>
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 /*
25 QUIC Wire Layout Specification : https://docs.google.com/document/d/1WJvyZflAO2pq77yOLbp9NsGjC1CHetAXV8I0fQe-B_U/
26
27 QUIC source code in Chromium : https://code.google.com/p/chromium/codesearch#chromium/src/net/quic/quic_utils.h&sq=package:chromium
28
29 */
30 #include "config.h"
31
32 #include <epan/packet.h>
33 #include <epan/prefs.h>
34
35 void proto_register_quic(void);
36 void proto_reg_handoff_quic(void);
37
38 static int proto_quic = -1;
39 static int hf_quic_puflags = -1;
40 static int hf_quic_puflags_vrsn = -1;
41 static int hf_quic_puflags_rst = -1;
42 static int hf_quic_puflags_cid = -1;
43 static int hf_quic_puflags_seq = -1;
44 static int hf_quic_puflags_rsv = -1;
45 static int hf_quic_cid = -1;
46 static int hf_quic_version = -1;
47 static int hf_quic_sequence = -1;
48 #if 0 /* Decode Private Flags is not yet ready... */
49 static int hf_quic_prflags = -1;
50 static int hf_quic_prflags_entropy = -1;
51 static int hf_quic_prflags_fecg = -1;
52 static int hf_quic_prflags_fec = -1;
53 static int hf_quic_prflags_rsv = -1;
54 #endif
55 static int hf_quic_payload = -1;
56
57 static guint g_quic_port = 80;
58 static guint g_quics_port = 443;
59
60 static gint ett_quic = -1;
61 static gint ett_quic_puflags = -1;
62 static gint ett_quic_prflags = -1;
63
64 #define QUIC_MIN_LENGTH 3
65
66 /**************************************************************************/
67 /*                      Public Flags                                      */
68 /**************************************************************************/
69 #define PUFLAGS_VRSN    0x01
70 #define PUFLAGS_RST     0x02
71 #define PUFLAGS_CID    0x0C
72 #define PUFLAGS_SEQ     0x30
73 #define PUFLAGS_RSV     0xC0
74
75 static const value_string puflags_cid_vals[] = {
76     { 0, "0 Byte" },
77     { 1, "1 Bytes" },
78     { 2, "4 Bytes" },
79     { 3, "8 Bytes" },
80     { 0, NULL }
81 };
82
83 static const value_string puflags_seq_vals[] = {
84     { 0, "1 Byte" },
85     { 1, "2 Bytes" },
86     { 2, "4 Bytes" },
87     { 3, "6 Bytes" },
88     { 0, NULL }
89 };
90
91 /**************************************************************************/
92 /*                      Private Flags                                     */
93 /**************************************************************************/
94 #define PRFLAGS_ENTROPY 0x01
95 #define PRFLAGS_FECG    0x02
96 #define PRFLAGS_FEC     0x04
97 #define PRFLAGS_RSV     0xF8
98
99 static int
100 dissect_quic_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
101         void *data _U_)
102 {
103     proto_item *ti, *ti_puflags/*, *ti_prflags, *expert_ti*/;
104     proto_tree *quic_tree, *puflags_tree/*, *prflags_tree*/;
105     guint offset = 0;
106     guint8 puflags, len_cid, len_seq;
107     guint64 cid, seq;
108
109     if (tvb_length(tvb) < QUIC_MIN_LENGTH)
110         return 0;
111
112     col_set_str(pinfo->cinfo, COL_PROTOCOL, "QUIC");
113
114     ti = proto_tree_add_item(tree, proto_quic, tvb, 0, -1, ENC_NA);
115     quic_tree = proto_item_add_subtree(ti, ett_quic);
116
117     /* Public Flags */
118     ti_puflags = proto_tree_add_item(quic_tree, hf_quic_puflags, tvb, offset, 1, ENC_LITTLE_ENDIAN);
119     puflags_tree = proto_item_add_subtree(ti_puflags, ett_quic_puflags);
120     proto_tree_add_item(puflags_tree, hf_quic_puflags_vrsn, tvb, offset, 1, ENC_NA);
121     proto_tree_add_item(puflags_tree, hf_quic_puflags_rst, tvb, offset, 1, ENC_NA);
122     proto_tree_add_item(puflags_tree, hf_quic_puflags_cid, tvb, offset, 1, ENC_LITTLE_ENDIAN);
123     proto_tree_add_item(puflags_tree, hf_quic_puflags_seq, tvb, offset, 1, ENC_LITTLE_ENDIAN);
124     proto_tree_add_item(puflags_tree, hf_quic_puflags_rsv, tvb, offset, 1, ENC_LITTLE_ENDIAN);
125
126     puflags = tvb_get_guint8(tvb, offset);
127
128     offset += 1;
129
130     /* CID */
131
132     /* Get len of CID (and CID), may be a more easy function to get the length... */
133     switch((puflags & PUFLAGS_CID) >> 2){
134         case 0:
135             len_cid = 0;
136             cid = 0;
137         break;
138         case 1:
139             len_cid = 1;
140             cid = tvb_get_guint8(tvb, offset);
141         break;
142         case 2:
143             len_cid = 4;
144             cid = tvb_get_letohl(tvb, offset);
145         break;
146         case 3:
147             len_cid = 8;
148             cid = tvb_get_letoh64(tvb, offset);
149         break;
150         default: /* It is only between 0..3 but Clang(Analyser) i don't like this... ;-) */
151             len_cid = 8;
152             cid = tvb_get_letoh64(tvb, offset);
153         break;
154     }
155
156     if (len_cid) {
157         proto_tree_add_item(quic_tree, hf_quic_cid, tvb, offset, len_cid, ENC_LITTLE_ENDIAN);
158         offset += len_cid;
159     }
160
161     /* Version */
162     if(puflags & PUFLAGS_VRSN){
163         proto_tree_add_item(quic_tree, hf_quic_version, tvb, offset, 4, ENC_ASCII|ENC_NA);
164         offset += 4;
165     }
166
167     /* Sequence */
168
169     /* Get len of sequence (and sequence), may be a more easy function to get the length... */
170     switch((puflags & PUFLAGS_SEQ) >> 4){
171         case 0:
172             len_seq = 1;
173             seq = tvb_get_guint8(tvb, offset);
174         break;
175         case 1:
176             len_seq = 2;
177             seq = tvb_get_letohs(tvb, offset);
178         break;
179         case 2:
180             len_seq = 4;
181             seq = tvb_get_letohl(tvb, offset);
182         break;
183         case 3:
184             len_seq = 6;
185             seq = tvb_get_letoh48(tvb, offset);
186         break;
187         default: /* It is only between 0..3 but Clang(Analyser) i don't like this... ;-) */
188             len_seq = 6;
189             seq = tvb_get_letoh48(tvb, offset);
190         break;
191     }
192     proto_tree_add_item(quic_tree, hf_quic_sequence, tvb, offset, len_seq, ENC_LITTLE_ENDIAN);
193     offset += len_seq;
194
195     col_add_fstr(pinfo->cinfo, COL_INFO, "CID: %" G_GINT64_MODIFIER "u, Seq: %" G_GINT64_MODIFIER "u", cid, seq);
196
197 #if 0 /* Decode Private Flags is not yet ready... */
198     /* Private Flags */
199     ti_prflags = proto_tree_add_item(quic_tree, hf_quic_prflags, tvb, offset, 1, ENC_LITTLE_ENDIAN);
200     prflags_tree = proto_item_add_subtree(ti_prflags, ett_quic_prflags);
201     proto_tree_add_item(prflags_tree, hf_quic_prflags_entropy, tvb, offset, 1, ENC_NA);
202     proto_tree_add_item(prflags_tree, hf_quic_prflags_fecg, tvb, offset, 1, ENC_NA);
203     proto_tree_add_item(prflags_tree, hf_quic_prflags_fec, tvb, offset, 1, ENC_NA);
204     proto_tree_add_item(prflags_tree, hf_quic_prflags_rsv, tvb, offset, 1, ENC_LITTLE_ENDIAN);
205     offset +=1;
206 #endif
207
208     /* Payload... (encrypted... TODO FIX !) */
209     proto_tree_add_item(quic_tree, hf_quic_payload, tvb, offset, -1, ENC_NA);
210
211     return offset;
212 }
213
214 static int
215 dissect_quic(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
216               void *data _U_)
217 {
218     return dissect_quic_common(tvb, pinfo, tree, NULL);
219 }
220
221 static int
222 dissect_quics(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
223               void *data _U_)
224 {
225     return dissect_quic_common(tvb, pinfo, tree, NULL);
226 }
227
228 void
229 proto_register_quic(void)
230 {
231     module_t *quic_module;
232
233     static hf_register_info hf[] = {
234         { &hf_quic_puflags,
235             { "Public Flags", "quic.puflags",
236                FT_UINT8, BASE_HEX, NULL, 0x0,
237               "Specifying per-packet public flags", HFILL }
238         },
239         { &hf_quic_puflags_vrsn,
240             { "Version", "quic.puflags.version",
241                FT_BOOLEAN, 8, TFS(&tfs_yes_no), PUFLAGS_VRSN,
242               "Signifies that this packet also contains the version of the QUIC protocol", HFILL }
243         },
244         { &hf_quic_puflags_rst,
245             { "Reset", "quic.puflags.reset",
246                FT_BOOLEAN, 8, TFS(&tfs_yes_no), PUFLAGS_RST,
247               "Signifies that this packet is a public reset packet", HFILL }
248         },
249         { &hf_quic_puflags_cid,
250             { "CID Length", "quic.puflags.cid",
251                FT_UINT8, BASE_HEX, VALS(puflags_cid_vals), PUFLAGS_CID,
252               "Signifies the Length of CID", HFILL }
253         },
254         { &hf_quic_puflags_seq,
255             { "Sequence Length", "quic.puflags.seq",
256                FT_UINT8, BASE_HEX, VALS(puflags_seq_vals), PUFLAGS_SEQ,
257               "Signifies the Length of Sequence", HFILL }
258         },
259         { &hf_quic_puflags_rsv,
260             { "Reserved", "quic.puflags.rsv",
261                FT_UINT8, BASE_HEX, NULL, PUFLAGS_RSV,
262               "Must be Zero", HFILL }
263         },
264         { &hf_quic_cid,
265             { "CID", "quic.cid",
266                FT_UINT64, BASE_DEC, NULL, 0x0,
267               "Connection ID 64 bit pseudo random number", HFILL }
268         },
269         { &hf_quic_version,
270             { "Version", "quic.version",
271                FT_STRING, BASE_NONE, NULL, 0x0,
272               "32 bit opaque tag that represents the version of the QUIC", HFILL }
273         },
274         { &hf_quic_sequence,
275             { "Sequence", "quic.sequence",
276                FT_UINT64, BASE_DEC, NULL, 0x0,
277               "The lower 8, 16, 32, or 48 bits of the sequence number", HFILL }
278         },
279 #if 0 /* Decode Private Flags is not yet ready... */
280         { &hf_quic_prflags,
281             { "Private Flags", "quic.prflags",
282                FT_UINT8, BASE_HEX, NULL, 0x0,
283               "Specifying per-packet Private flags", HFILL }
284         },
285         { &hf_quic_prflags_entropy,
286             { "Entropy", "quic.prflags.entropy",
287                FT_BOOLEAN, 8, TFS(&tfs_yes_no), PRFLAGS_ENTROPY,
288               "For data packets, signifies that this packet contains the 1 bit of entropy, for fec packets, contains the xor of the entropy of protected packets", HFILL }
289         },
290         { &hf_quic_prflags_fecg,
291             { "FEC Group", "quic.prflags.fecg",
292                FT_BOOLEAN, 8, TFS(&tfs_yes_no), PRFLAGS_FECG,
293               "Indicates whether the fec byte is present.", HFILL }
294         },
295         { &hf_quic_prflags_fec,
296             { "FEC", "quic.prflags.fec",
297                FT_BOOLEAN, 8, TFS(&tfs_yes_no), PRFLAGS_FEC,
298               "Signifies that this packet represents an FEC packet", HFILL }
299         },
300         { &hf_quic_prflags_rsv,
301             { "Reserved", "quic.prflags.rsv",
302                FT_UINT8, BASE_HEX, NULL, PRFLAGS_RSV,
303               "Must be Zero", HFILL }
304         },
305 #endif
306
307         { &hf_quic_payload,
308             { "Payload", "quic.payload",
309                FT_BYTES, BASE_NONE, NULL, 0x0,
310               "Quic Payload..", HFILL }
311         },
312
313     };
314
315
316     static gint *ett[] = {
317         &ett_quic,
318         &ett_quic_puflags,
319         &ett_quic_prflags
320     };
321
322     proto_quic = proto_register_protocol("QUIC (Quick UDP Internet Connections)",
323             "QUIC", "quic");
324
325     proto_register_field_array(proto_quic, hf, array_length(hf));
326     proto_register_subtree_array(ett, array_length(ett));
327
328     quic_module = prefs_register_protocol(proto_quic, proto_reg_handoff_quic);
329
330
331     prefs_register_uint_preference(quic_module, "udp.quic.port", "QUIC UDP Port",
332             "QUIC UDP port if other than the default",
333             10, &g_quic_port);
334
335     prefs_register_uint_preference(quic_module, "udp.quics.port", "QUICS UDP Port",
336             "QUICS (Secure) UDP port if other than the default",
337             10, &g_quics_port);
338 }
339
340 void
341 proto_reg_handoff_quic(void)
342 {
343     static gboolean initialized = FALSE;
344     static dissector_handle_t quic_handle;
345     static dissector_handle_t quics_handle;
346     static int current_quic_port;
347     static int current_quics_port;
348
349     if (!initialized) {
350         quic_handle = new_create_dissector_handle(dissect_quic,
351                 proto_quic);
352         quics_handle = new_create_dissector_handle(dissect_quics,
353                 proto_quic);
354         initialized = TRUE;
355
356     } else {
357         dissector_delete_uint("udp.port", current_quic_port, quic_handle);
358         dissector_delete_uint("udp.port", current_quics_port, quics_handle);
359     }
360
361     current_quic_port = g_quic_port;
362     current_quics_port = g_quics_port;
363
364
365     dissector_add_uint("udp.port", current_quic_port, quic_handle);
366     dissector_add_uint("udp.port", current_quics_port, quics_handle);
367 }
368
369
370 /*
371  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
372  *
373  * Local variables:
374  * c-basic-offset: 4
375  * tab-width: 8
376  * indent-tabs-mode: nil
377  * End:
378  *
379  * vi: set shiftwidth=4 tabstop=8 expandtab:
380  * :indentSize=4:tabSize=8:noTabs=true:
381  */