Have separate fields for volume, long file name, short file name, and
[obnox/wireshark/wip.git] / packet-vines.c
1 /* packet-vines.c
2  * Routines for Banyan VINES protocol packet disassembly
3  *
4  * $Id: packet-vines.c,v 1.40 2002/04/24 06:03:34 guy Exp $
5  *
6  * Don Lafontaine <lafont02@cn.ca>
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
11  * Joerg Mayer <jmayer@loplof.de>
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 #ifdef HAVE_SYS_TYPES_H
33 #include <sys/types.h>
34 #endif
35
36 #ifdef HAVE_NETINET_IN_H
37 #include <netinet/in.h>
38 #endif
39
40 #include <string.h>
41 #include <glib.h>
42 #include <epan/packet.h>
43 #include "packet-vines.h"
44 #include "etypes.h"
45 #include "ppptypes.h"
46 #include "ipproto.h"
47
48 #define UDP_PORT_VINES  573
49
50 static int proto_vines = -1;
51 static int hf_vines_protocol = -1;
52
53 static gint ett_vines = -1;
54
55 static int proto_vines_frp = -1;
56
57 static gint ett_vines_frp = -1;
58
59 static int proto_vines_spp = -1;
60
61 static gint ett_vines_spp = -1;
62
63 static void dissect_vines_frp(tvbuff_t *, packet_info *, proto_tree *);
64 #if 0
65 static void dissect_vines_arp(tvbuff_t *, packet_info *, proto_tree *);
66 static void dissect_vines_icp(tvbuff_t *, packet_info *, proto_tree *);
67 static void dissect_vines_ipc(tvbuff_t *, packet_info *, proto_tree *);
68 static void dissect_vines_rtp(tvbuff_t *, packet_info *, proto_tree *);
69 #endif
70 static void dissect_vines_spp(tvbuff_t *, packet_info *, proto_tree *);
71 static void dissect_vines(tvbuff_t *, packet_info *, proto_tree *);
72
73 void
74 capture_vines(packet_counts *ld)
75 {
76         ld->vines++;
77 }
78
79 static dissector_handle_t vines_handle;
80 static dissector_handle_t data_handle;
81
82 /* AFAIK Vines FRP (Fragmentation Protocol) is used on all media except
83  * Ethernet and TR (and probably FDDI) - Fragmentation on these media types
84  * is not possible
85  * FIXME: Do we need to use this header with PPP too?
86  */
87 static void
88 dissect_vines_frp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
89 {
90         guint8   vines_frp_ctrl;
91         proto_tree *vines_frp_tree;
92         proto_item *ti;
93         gchar   frp_flags_str[32];
94         tvbuff_t *next_tvb;
95
96         if (check_col(pinfo->cinfo, COL_PROTOCOL))
97                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Vines FRP");
98         if (check_col(pinfo->cinfo, COL_INFO))
99                 col_clear(pinfo->cinfo, COL_INFO);
100
101         if (tree) {
102                 ti = proto_tree_add_item(tree, proto_vines_frp, tvb, 0, 2,
103                     FALSE);
104                 vines_frp_tree = proto_item_add_subtree(ti, ett_vines_frp);
105
106                 vines_frp_ctrl = tvb_get_guint8(tvb, 0);
107   
108                 /*
109                  * 1: first fragment of vines packet
110                  * 2: last fragment of vines packet
111                  * 4 ... 80: unused
112                  */
113                 switch (vines_frp_ctrl) {
114
115                 case 0:
116                         strcpy(frp_flags_str, "middle");
117                         break;
118
119                 case 1:
120                         strcpy(frp_flags_str, "first");
121                         break;
122
123                 case 2:
124                         strcpy(frp_flags_str, "last");
125                         break;
126
127                 case 3:
128                         strcpy(frp_flags_str, "only");
129                         break;
130
131                 default:
132                         strcpy(frp_flags_str, "please report: unknown");
133                         break;
134                 }
135   
136                 proto_tree_add_text(vines_frp_tree, tvb, 0, 1, 
137                                     "Control Flags: 0x%02x = %s fragment", 
138                                     vines_frp_ctrl, frp_flags_str);
139
140                 proto_tree_add_text(vines_frp_tree, tvb, 1, 1, 
141                                     "Sequence Number: 0x%02x",
142                                     tvb_get_guint8(tvb, 1));
143         }
144
145         /* Decode the "real" Vines now */
146         next_tvb = tvb_new_subset(tvb, 2, -1, -1);
147         call_dissector(vines_handle, next_tvb, pinfo, tree);
148 }
149
150 void
151 proto_register_vines_frp(void)
152 {
153         static gint *ett[] = {
154                 &ett_vines_frp,
155         };
156
157         proto_vines_frp = proto_register_protocol(
158             "Banyan Vines Fragmentation Protocol", "Vines FRP", "vines_frp");
159         proto_register_subtree_array(ett, array_length(ett));
160 }
161
162 void
163 proto_reg_handoff_vines_frp(void)
164 {
165         dissector_handle_t vines_frp_handle;
166
167         vines_frp_handle = create_dissector_handle(dissect_vines_frp,
168             proto_vines_frp);
169         dissector_add("ip.proto", IP_PROTO_VINES, vines_frp_handle);
170
171         /* XXX: AFAIK, src and dst port must be the same; should
172            the dissector check for that? */
173         dissector_add("udp.port", UDP_PORT_VINES, vines_frp_handle);
174 }
175
176 static dissector_table_t vines_dissector_table;
177
178 static void
179 dissect_vines(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
180 {
181         int         offset = 0;
182         e_vip       viph;
183         proto_tree *vip_tree;
184         proto_item *ti;
185 /*      gchar      tos_str[32]; */
186         const guint8     *dst_addr, *src_addr;
187         int  is_broadcast = 0;
188         int  hops = 0;
189         tvbuff_t *next_tvb;
190
191         if (check_col(pinfo->cinfo, COL_PROTOCOL))
192                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Vines");
193         if (check_col(pinfo->cinfo, COL_INFO))
194                 col_clear(pinfo->cinfo, COL_INFO);
195
196         /* To do: check for runts, errs, etc. */
197
198         /* Avoids alignment problems on many architectures. */
199         tvb_memcpy(tvb, (guint8 *)&viph, offset, sizeof(e_vip));
200
201         viph.vip_chksum = ntohs(viph.vip_chksum);
202         viph.vip_pktlen = ntohs(viph.vip_pktlen);
203         viph.vip_dnet = ntohl(viph.vip_dnet);
204         viph.vip_dsub = ntohs(viph.vip_dsub);
205         viph.vip_snet = ntohl(viph.vip_snet);
206         viph.vip_ssub = ntohs(viph.vip_ssub);
207
208         switch (viph.vip_proto) {
209         case VIP_PROTO_IPC:
210                 if (check_col(pinfo->cinfo, COL_PROTOCOL))
211                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "Vines IPC");
212                 if (check_col(pinfo->cinfo, COL_INFO))
213                         col_add_fstr(pinfo->cinfo, COL_INFO, "IPC (%02x)", viph.vip_proto);
214                 break;
215         case VIP_PROTO_SPP:      
216                 if (check_col(pinfo->cinfo, COL_PROTOCOL))
217                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "Vines SPP");
218                 if (check_col(pinfo->cinfo, COL_INFO))
219                         col_add_fstr(pinfo->cinfo, COL_INFO, "SPP (%02x)", viph.vip_proto);
220                 break;
221         case VIP_PROTO_ARP:
222                 if (check_col(pinfo->cinfo, COL_PROTOCOL))
223                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "Vines ARP");
224                 if (check_col(pinfo->cinfo, COL_INFO))
225                         col_add_fstr(pinfo->cinfo, COL_INFO, "ARP (%02x)", viph.vip_proto);
226                 break;
227         case VIP_PROTO_RTP:
228                 if (check_col(pinfo->cinfo, COL_PROTOCOL))
229                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "Vines RTP");
230                 if (check_col(pinfo->cinfo, COL_INFO))
231                         col_add_fstr(pinfo->cinfo, COL_INFO, "RTP (%02x)", viph.vip_proto);
232                 break;
233         case VIP_PROTO_ICP:
234                 if (check_col(pinfo->cinfo, COL_PROTOCOL))
235                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "Vines ICP");
236                 if (check_col(pinfo->cinfo, COL_INFO))
237                         col_add_fstr(pinfo->cinfo, COL_INFO, "ICP (%02x)", viph.vip_proto);
238                 break;
239         default:
240                 if (check_col(pinfo->cinfo, COL_PROTOCOL))
241                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "Vines IP");
242                 if (check_col(pinfo->cinfo, COL_INFO))
243                         col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown VIP protocol (%02x)", 
244                                      viph.vip_proto);
245         }
246
247         src_addr = tvb_get_ptr(tvb, offset+12, VINES_ADDR_LEN);
248         SET_ADDRESS(&pinfo->net_src, AT_VINES, VINES_ADDR_LEN, src_addr);
249         SET_ADDRESS(&pinfo->src, AT_VINES, VINES_ADDR_LEN, src_addr);
250         dst_addr = tvb_get_ptr(tvb, offset+6, VINES_ADDR_LEN);
251         SET_ADDRESS(&pinfo->net_dst, AT_VINES, VINES_ADDR_LEN,dst_addr);
252         SET_ADDRESS(&pinfo->dst, AT_VINES, VINES_ADDR_LEN, dst_addr);
253
254         /* helpers to decode flags */
255         /* FIXME: Not used yet */
256         if ((viph.vip_dnet == 0xffffffff) && (viph.vip_dsub == 0xffff)) {
257                 is_broadcast = 1;
258         }
259         hops = viph.vip_tctl & 0xf; 
260  
261         /*
262         viph.ip_tos = IPTOS_TOS(viph.ip_tos);
263         switch (viph.ip_tos) {
264         case IPTOS_NONE:
265                 strcpy(tos_str, "None");
266                 break;
267         case IPTOS_LOWDELAY:
268                 strcpy(tos_str, "Minimize delay");
269                 break;
270         case IPTOS_THROUGHPUT:
271                 strcpy(tos_str, "Maximize throughput");
272                 break;
273         case IPTOS_RELIABILITY:
274                 strcpy(tos_str, "Maximize reliability");
275                 break;
276         case IPTOS_LOWCOST:
277                 strcpy(tos_str, "Minimize cost");
278                 break;
279         default:
280                 strcpy(tos_str, "Unknon.  Malformed?");
281                 break;
282         }
283         */
284
285         if (tree) {
286                 ti = proto_tree_add_protocol_format(tree, proto_vines, tvb, 
287                                                     offset, (viph.vip_pktlen),
288                                                     "Vines IP");
289                 vip_tree = proto_item_add_subtree(ti, ett_vines);
290                 proto_tree_add_text(vip_tree, tvb, offset,      2, 
291                                     "Packet checksum: 0x%04x", 
292                                     viph.vip_chksum);
293                 proto_tree_add_text(vip_tree, tvb, offset +  2, 2, 
294                                     "Packet length: 0x%04x (%d)", 
295                                     viph.vip_pktlen, viph.vip_pktlen); 
296                 proto_tree_add_text(vip_tree, tvb, offset +  4, 1, 
297                                     "Transport control: 0x%02x",
298                                     viph.vip_tctl);
299                 proto_tree_add_uint(vip_tree, hf_vines_protocol, tvb, 
300                                     offset +  5, 1, 
301                                     viph.vip_proto);
302                 proto_tree_add_text(vip_tree, tvb, offset +  6, 
303                                     VINES_ADDR_LEN, 
304                                     "Destination: %s",
305                                     vines_addr_to_str(dst_addr));
306                 proto_tree_add_text(vip_tree, tvb, offset +  12, 
307                                     VINES_ADDR_LEN, 
308                                     "Source: %s",
309                                     vines_addr_to_str(src_addr));
310
311         }
312
313         offset += 18;
314         next_tvb = tvb_new_subset(tvb, offset, -1, -1);
315         if (!dissector_try_port(vines_dissector_table, viph.vip_proto,
316             next_tvb, pinfo, tree))
317                 call_dissector(data_handle,next_tvb, pinfo, tree);
318 }
319
320 void
321 proto_register_vines(void)
322 {
323         static gint *ett[] = {
324                 &ett_vines,
325         };
326
327         static hf_register_info hf[] = {
328           { &hf_vines_protocol,
329             { "Protocol",                       "vines.protocol",
330               FT_UINT8,         BASE_HEX,       NULL,   0x0,
331               "Vines protocol", HFILL }}
332         };
333
334         proto_vines = proto_register_protocol("Banyan Vines", "Vines", "vines");
335         proto_register_field_array(proto_vines, hf, array_length(hf));
336         proto_register_subtree_array(ett, array_length(ett));
337
338         /* subdissector code */
339         vines_dissector_table = register_dissector_table("vines.proto",
340             "Vines protocol", FT_UINT8, BASE_HEX);
341
342         register_dissector("vines", dissect_vines, proto_vines);
343         vines_handle = find_dissector("vines");
344 }
345
346 void
347 proto_reg_handoff_vines(void)
348 {
349         dissector_add("ethertype", ETHERTYPE_VINES, vines_handle);
350         dissector_add("ppp.protocol", PPP_VINES, vines_handle);
351         data_handle = find_dissector("data");
352 }
353
354 static void
355 dissect_vines_spp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
356 {
357         int          offset = 0;
358         e_vspp       viph;
359         proto_tree *vspp_tree;
360         proto_item *ti;
361
362         if (check_col(pinfo->cinfo, COL_PROTOCOL))
363                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "VSPP");
364         if (check_col(pinfo->cinfo, COL_INFO))
365                 col_clear(pinfo->cinfo, COL_INFO);
366
367         /* To do: check for runts, errs, etc. */
368
369         /* Avoids alignment problems on many architectures. */
370         tvb_memcpy(tvb, (guint8 *)&viph, offset, sizeof(e_vspp));
371
372         viph.vspp_sport = ntohs(viph.vspp_sport);
373         viph.vspp_dport = ntohs(viph.vspp_dport);
374         viph.vspp_lclid = ntohs(viph.vspp_lclid);
375         viph.vspp_rmtid = ntohs(viph.vspp_rmtid);
376
377         switch (viph.vspp_pkttype) {
378         case VSPP_PKTTYPE_DATA:      
379                 if (check_col(pinfo->cinfo, COL_PROTOCOL))
380                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "VSPP Data");
381                 break;
382         case VSPP_PKTTYPE_DISC:      
383                 if (check_col(pinfo->cinfo, COL_PROTOCOL))
384                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "VSPP Disconnect");
385                 break;
386         case VSPP_PKTTYPE_PROBE:      
387                 if (check_col(pinfo->cinfo, COL_PROTOCOL))
388                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "VSPP Probe");
389                 break;
390         case VSPP_PKTTYPE_ACK:
391                 if (check_col(pinfo->cinfo, COL_PROTOCOL))
392                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "VSPP Ack");
393                 break;
394         default:
395                 if (check_col(pinfo->cinfo, COL_PROTOCOL))
396                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "VSPP Unknown");
397         }
398
399         if (check_col(pinfo->cinfo, COL_INFO))
400                 col_add_fstr(pinfo->cinfo, COL_INFO, 
401                              "NS=%04x NR=%04x Window=%04x RID=%04x LID=%04x D=%04x S=%04x", 
402                              viph.vspp_seqno, viph.vspp_ack, viph.vspp_win, 
403                              viph.vspp_rmtid, viph.vspp_lclid, viph.vspp_dport,
404                              viph.vspp_sport);
405
406         /*
407         iph.ip_tos = IPTOS_TOS(iph.ip_tos);
408         switch (iph.ip_tos) {
409         case IPTOS_NONE:
410                 strcpy(tos_str, "None");
411                 break;
412         case IPTOS_LOWDELAY:
413                 strcpy(tos_str, "Minimize delay");
414                 break;
415         case IPTOS_THROUGHPUT:
416                 strcpy(tos_str, "Maximize throughput");
417                 break;
418         case IPTOS_RELIABILITY:
419                 strcpy(tos_str, "Maximize reliability");
420                 break;
421         case IPTOS_LOWCOST:
422                 strcpy(tos_str, "Minimize cost");
423                 break;
424         default:
425                 strcpy(tos_str, "Unknon.  Malformed?");
426                 break;
427         }
428         */ 
429
430         if (tree) {
431                 ti = proto_tree_add_item(tree, proto_vines_spp, tvb, offset,
432                     sizeof(viph), FALSE);
433                 vspp_tree = proto_item_add_subtree(ti, ett_vines_spp);
434                 proto_tree_add_text(vspp_tree, tvb, offset,      2, 
435                                     "Source port: 0x%04x", viph.vspp_sport);
436                 proto_tree_add_text(vspp_tree, tvb, offset + 2,  2, 
437                                     "Destination port: 0x%04x",
438                                     viph.vspp_dport); 
439                 proto_tree_add_text(vspp_tree, tvb, offset + 4,  1, 
440                                     "Packet type: 0x%02x", viph.vspp_pkttype);
441                 proto_tree_add_text(vspp_tree, tvb, offset + 5,  1, 
442                                     "Control: 0x%02x", viph.vspp_control);
443                 proto_tree_add_text(vspp_tree, tvb, offset + 6,  2, 
444                                     "Local Connection ID: 0x%04x",
445                                     viph.vspp_lclid);
446                 proto_tree_add_text(vspp_tree, tvb, offset + 8,  2,
447                                     "Remote Connection ID: 0x%04x", 
448                                     viph.vspp_rmtid);
449                 proto_tree_add_text(vspp_tree, tvb, offset + 10, 2,
450                                     "Sequence number: 0x%04x", 
451                                     viph.vspp_seqno);
452                 proto_tree_add_text(vspp_tree, tvb, offset + 12, 2,
453                                     "Ack number: 0x%04x", viph.vspp_ack);
454                 proto_tree_add_text(vspp_tree, tvb, offset + 14, 2,
455                                     "Window: 0x%04x", viph.vspp_win);
456         }
457         offset += 16; /* sizeof SPP */
458         call_dissector(data_handle,tvb_new_subset(tvb, offset,-1,tvb_reported_length_remaining(tvb,offset)), pinfo, tree);
459 }
460
461 void
462 proto_register_vines_spp(void)
463 {
464         static gint *ett[] = {
465                 &ett_vines_spp,
466         };
467
468         proto_vines_spp = proto_register_protocol("Banyan Vines SPP",
469             "Vines SPP", "vines_spp");
470         proto_register_subtree_array(ett, array_length(ett));
471 }
472
473 void
474 proto_reg_handoff_vines_spp(void)
475 {
476         dissector_handle_t vines_spp_handle;
477
478         vines_spp_handle = create_dissector_handle(dissect_vines_spp,
479             proto_vines_spp);
480         dissector_add("vines.proto", VIP_PROTO_SPP, vines_spp_handle);
481 }