073924e2de6e5bd6e45aee3d45763ca3355dd4c1
[obnox/wireshark/wip.git] / epan / dissectors / packet-kdp.c
1 /* packet-kdp.c
2  * Routines for KDP (Kontiki Delivery Protocol) packet disassembly
3  *
4  * $Id$
5  *
6  * Copyright (c) 2008 by Kontiki Inc.
7  *                    Wade Hennessey <wade@kontiki.com>
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1999 Gerald Combs
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <epan/packet.h>
33 #include <glib.h>
34
35 #define KDP_PORT 19948
36 #define BUFFER_SIZE 80
37 static int proto_kdp = -1;
38 static gint ett_kdp = -1;
39 static gint ett_kdp_flags = -1;
40
41 static int hf_kdp_version = -1;
42 static int hf_kdp_headerlen = -1;
43 static int hf_kdp_flags = -1;
44 static int hf_kdp_errors = -1;
45 static int hf_kdp_destflowid = -1;
46 static int hf_kdp_srcflowid = -1;
47 static int hf_kdp_sequence = -1;
48 static int hf_kdp_ack = -1;
49 static int hf_kdp_maxsegmentsize = -1;
50 static int hf_kdp_optionnumber = -1;
51 static int hf_kdp_optionlen = -1;
52 static int hf_kdp_option1 = -1;
53 static int hf_kdp_option2 = -1;
54 static int hf_kdp_fragment = -1;
55 static int hf_kdp_fragtotal = -1;
56 static int hf_kdp_body = -1;
57 static int hf_kdp_xml_body = -1;
58
59 #define KDP_DROP_FLAG (1 << 0)
60 #define KDP_SYN_FLAG  (1 << 1)
61 #define KDP_ACK_FLAG  (1 << 2)
62 #define KDP_RST_FLAG  (1 << 3)
63 #define KDP_BCST_FLAG (1 << 4)
64 #define KDP_DUP_FLAG  (1 << 5)
65
66 static int hf_kdp_drop_flag = -1;
67 static int hf_kdp_syn_flag = -1;
68 static int hf_kdp_ack_flag = -1;
69 static int hf_kdp_rst_flag = -1;
70 static int hf_kdp_bcst_flag = -1;
71 static int hf_kdp_dup_flag = -1;
72
73 static void dissect_kdp(tvbuff_t *tvb,
74                         packet_info *pinfo,
75                         proto_tree *tree) {
76   guint body_len;
77   guint8 version = 0;
78   guint8 header_len = 0;
79   guint8 packet_flags = 0;
80   guint8 packet_errors = 0;
81   guint32 sequence_number = -1;
82   guint32 ack_number = -1; 
83   guint32 src_flowid = -1; 
84   int offset;
85
86   if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
87     col_set_str(pinfo->cinfo, COL_PROTOCOL, "KDP");
88   }
89   if (check_col(pinfo->cinfo, COL_INFO)) {
90     col_clear(pinfo->cinfo, COL_INFO);
91   }
92   if (tree) {
93     proto_item *ti;
94     proto_tree *kdp_tree, *flags_tree;
95     ti = NULL;
96     kdp_tree = NULL;
97     flags_tree = NULL;
98
99     ti = proto_tree_add_item(tree, proto_kdp, tvb, 0, -1, FALSE);
100     kdp_tree = proto_item_add_subtree(ti, ett_kdp);
101
102     version = tvb_get_guint8(tvb, 0);
103     if (version != 2) {
104       /* Version other than 2 is really SDDP in UDP */
105       proto_tree_add_item(kdp_tree, hf_kdp_xml_body, tvb, 0, -1, FALSE);
106     } else {
107       header_len = tvb_get_guint8(tvb, 1) * 4;
108       body_len = tvb_reported_length(tvb);
109       if (header_len > body_len) {
110         body_len = 0;           /* malformed packet */
111       } else {
112         body_len = body_len - header_len;
113       }
114       packet_flags = tvb_get_guint8(tvb, 2);
115       packet_errors = tvb_get_guint8(tvb, 3);
116       proto_tree_add_item(kdp_tree, hf_kdp_version, tvb, 0, 1, FALSE);
117       proto_tree_add_item(kdp_tree, hf_kdp_headerlen, tvb, 1, 1, FALSE);
118       ti = proto_tree_add_item(kdp_tree, hf_kdp_flags, tvb, 2, 1, FALSE);
119       flags_tree = proto_item_add_subtree(ti, ett_kdp_flags);
120
121       proto_tree_add_item(flags_tree, hf_kdp_drop_flag, tvb, 2, 1, FALSE);
122       proto_tree_add_item(flags_tree, hf_kdp_syn_flag, tvb, 2, 1, FALSE);
123       proto_tree_add_item(flags_tree, hf_kdp_ack_flag, tvb, 2, 1, FALSE);
124       proto_tree_add_item(flags_tree, hf_kdp_rst_flag, tvb, 2, 1, FALSE);
125       proto_tree_add_item(flags_tree, hf_kdp_bcst_flag, tvb, 2, 1, FALSE);
126       proto_tree_add_item(flags_tree, hf_kdp_dup_flag, tvb, 2, 1, FALSE);
127
128       proto_tree_add_item(kdp_tree, hf_kdp_errors, tvb, 3, 1, FALSE);  
129
130       if (header_len > 4) {
131         offset = 4;
132         if (packet_flags & KDP_ACK_FLAG) {
133           proto_tree_add_item(kdp_tree, hf_kdp_destflowid, tvb, offset, 4, FALSE);
134           offset = offset + 4;
135         }
136
137         if (packet_flags & (KDP_SYN_FLAG | KDP_BCST_FLAG)) {
138           proto_tree_add_item(kdp_tree, hf_kdp_srcflowid, tvb, offset, 4, FALSE);
139           src_flowid = tvb_get_ntohl(tvb, offset);
140           offset = offset + 4;
141         }
142
143         proto_tree_add_item(kdp_tree, hf_kdp_sequence, tvb, offset, 4, FALSE);
144         sequence_number = tvb_get_ntohl(tvb, offset);
145         offset = offset + 4;
146
147         if (packet_flags & KDP_ACK_FLAG) {
148           proto_tree_add_item(kdp_tree, hf_kdp_ack, tvb, offset, 4, FALSE);
149           ack_number = tvb_get_ntohl(tvb, offset);
150           offset = offset + 4;
151         }
152         if (packet_flags & KDP_SYN_FLAG) {
153           proto_tree_add_item(kdp_tree,
154                               hf_kdp_maxsegmentsize, tvb, offset, 4, FALSE);
155           offset = offset + 4;
156         }
157
158         while (offset < ((body_len > 0) ? header_len - 4 : header_len)) {
159           guint8 option_number;
160
161           option_number = tvb_get_guint8(tvb, offset);
162       
163           proto_tree_add_item(kdp_tree,
164                               hf_kdp_optionnumber, tvb, offset, 1, FALSE);
165           offset = offset + 1;
166           if (option_number > 0) {
167             proto_tree_add_item(kdp_tree,
168                                 hf_kdp_optionlen, tvb, offset, 1, FALSE);
169             offset = offset + 1;
170           }
171
172           switch (option_number) {
173           case 0:
174             break;
175           case 1:
176             proto_tree_add_item(kdp_tree, hf_kdp_option1, tvb, offset, 2, FALSE);
177             offset = offset + 2;
178             break;
179           case 2:
180             proto_tree_add_item(kdp_tree, hf_kdp_option2, tvb, offset, 2, FALSE);
181             offset = offset + 2;
182             break;
183           default: body_len = 0; /* Invalid option - ignore rest of packet */
184           }
185         }
186       
187         if (body_len > 0) {
188           proto_tree_add_item(kdp_tree, hf_kdp_fragment, tvb, offset, 2, FALSE);
189           offset = offset + 2;
190
191           proto_tree_add_item(kdp_tree, hf_kdp_fragtotal, tvb, offset, 2, FALSE);
192           offset = offset + 2;
193
194           proto_tree_add_item(kdp_tree, hf_kdp_body, tvb, offset, -1, FALSE);
195         }
196       }
197     }
198   }
199   /* Now that we know sequence number and optional ack number, we can
200      print more detailed summary info */
201   if (check_col(pinfo->cinfo, COL_INFO)) {
202     if (version != 2) {
203       col_add_fstr(pinfo->cinfo, COL_INFO, "SDDP message");
204     } else {
205       char ack_string[BUFFER_SIZE];
206       char seq_num_string[BUFFER_SIZE];
207       char src_flowid_string[BUFFER_SIZE];
208
209       if (packet_flags & KDP_ACK_FLAG) {
210         g_snprintf(ack_string, sizeof(ack_string), "ACK=%x ", ack_number);
211       } else {
212         ack_string[0] = '\0';
213       }
214       if (header_len > 4) {
215         g_snprintf(seq_num_string, sizeof(seq_num_string), "SEQ=%x ", sequence_number);
216       } else {
217         seq_num_string[0] = '\0';
218       }
219       if (packet_flags & (KDP_SYN_FLAG | KDP_BCST_FLAG)) {
220         g_snprintf(src_flowid_string, sizeof(src_flowid_string), "SRC_FLOWID=%x ", src_flowid);
221       } else {
222         src_flowid_string[0] = '\0';
223       }
224       col_add_fstr(pinfo->cinfo, COL_INFO, "%s%s%s%s%s%s%s%serrors=%d",
225                    ((packet_flags & KDP_DROP_FLAG) ? "DROP " : ""),
226                    ((packet_flags & KDP_SYN_FLAG) ? "SYN " : ""),
227                    ((packet_flags & KDP_RST_FLAG) ? "RST " : ""),
228                    ((packet_flags & KDP_BCST_FLAG) ? "BCST " : ""),
229                    ((packet_flags & KDP_DUP_FLAG) ? "DUP " : ""),
230                    ack_string,
231                    seq_num_string,
232                    src_flowid_string,
233                    packet_errors);
234     }
235   }
236 }
237
238 void proto_register_kdp(void) {
239
240   static hf_register_info hf[] = {
241     { &hf_kdp_version,
242       {"KDP version", "kdp.version",
243        FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL}
244     },
245     { &hf_kdp_headerlen,
246       {"KDP header len", "kdp.headerlen",
247        FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL}
248     },
249     { &hf_kdp_flags,
250       {"KDP flags", "kdp.flags",
251        FT_UINT8, BASE_HEX, NULL, 0x0, "", HFILL}
252     },
253     { &hf_kdp_drop_flag,
254       {"KDP DROP Flag", "kdp.flags.drop",
255        FT_BOOLEAN, 8, NULL, KDP_DROP_FLAG, "", HFILL}
256     },
257     { &hf_kdp_syn_flag,
258       {"KDP SYN Flag", "kdp.flags.syn",
259        FT_BOOLEAN, 8, NULL, KDP_SYN_FLAG, "", HFILL}
260     },
261     { &hf_kdp_ack_flag,
262       {"KDP ACK Flag", "kdp.flags.ack",
263        FT_BOOLEAN, 8, NULL, KDP_ACK_FLAG, "", HFILL}
264     },
265     { &hf_kdp_rst_flag,
266       {"KDP RST Flag", "kdp.flags.rst",
267        FT_BOOLEAN, 8, NULL, KDP_RST_FLAG, "", HFILL}
268     },
269     { &hf_kdp_bcst_flag,
270       {"KDP BCST Flag", "kdp.flags.bcst",
271        FT_BOOLEAN, 8, NULL, KDP_BCST_FLAG, "", HFILL}
272     },
273     { &hf_kdp_dup_flag,
274       {"KDP DUP Flag", "kdp.flags.dup",
275        FT_BOOLEAN, 8, NULL, KDP_DUP_FLAG, "", HFILL}
276     },
277     { &hf_kdp_errors,
278       {"KDP errors", "kdp.errors",
279        FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL}
280     },
281     { &hf_kdp_destflowid,
282       { "DestFlowID", "kdp.destflowid",
283         FT_UINT32, BASE_HEX, NULL, 0x0, "", HFILL}
284     },
285     { &hf_kdp_srcflowid,
286       { "SrcFlowID", "kdp.srcflowid",
287         FT_UINT32, BASE_HEX, NULL, 0x0, "", HFILL}
288     },
289     { &hf_kdp_sequence,
290       { "Sequence", "kdp.sequence",
291         FT_UINT32, BASE_HEX, NULL, 0x0, "", HFILL}
292     },
293     { &hf_kdp_ack,
294       { "Ack", "kdp.ack",
295         FT_UINT32, BASE_HEX, NULL, 0x0, "", HFILL}
296     },
297     { &hf_kdp_maxsegmentsize,
298       { "MaxSegmentSize", "kdp.maxsegmentsize",
299         FT_UINT32, BASE_HEX, NULL, 0x0, "", HFILL}
300     },
301     { &hf_kdp_optionnumber,
302       { "Option Number", "kdp.optionnumber",
303         FT_UINT8, BASE_HEX, NULL, 0x0, "", HFILL}
304     },
305     { &hf_kdp_optionlen,
306       { "Option Len", "kdp.option",
307         FT_UINT8, BASE_HEX, NULL, 0x0, "", HFILL}
308     },
309     { &hf_kdp_option1,
310       { "Option1 - Max Window", "kdp.option1",
311         FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL}
312     },
313     { &hf_kdp_option2,
314       { "Option2 - TCP Fraction", "kdp.option2",
315         FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL}
316     },
317     { &hf_kdp_fragment,
318       { "Fragment", "kdp.fragment",
319         FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL}
320     },
321     { &hf_kdp_fragtotal,
322       { "FragTotal", "kdp.fragtotal",
323         FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL}
324     },
325     { &hf_kdp_body,
326       { "Encrypted Body", "kdp.body",
327         FT_BYTES, BASE_HEX, NULL, 0x0, "", HFILL}
328     },
329     { &hf_kdp_xml_body,
330       { "XML Body", "kdp.body",
331         FT_STRING, BASE_NONE, NULL, 0x0, "", HFILL}
332     }
333   };
334
335   /* Setup protocol subtree array */
336   static gint *ett[] = {
337     &ett_kdp, &ett_kdp_flags
338   };
339
340   proto_kdp = proto_register_protocol("Kontiki Delivery Protocol", /* name */
341                                       "KDP",            /* short name */
342                                       "kdp");           /* abbrev */
343   proto_register_field_array(proto_kdp, hf, array_length(hf));
344   proto_register_subtree_array(ett, array_length(ett));
345 }
346
347 void
348 proto_reg_handoff_kdp(void) {
349   dissector_handle_t kdp_handle;
350   kdp_handle = create_dissector_handle(dissect_kdp, proto_kdp);
351   dissector_add("udp.port", KDP_PORT, kdp_handle);
352 }
353