Move a pile of protocol-related headers from the top-level source
[obnox/wireshark/wip.git] / epan / dissectors / packet-vj.c
1 /* packet-vj.c
2  * Routines for Van Jacobson header decompression.  (See RFC 1144.)
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  *
9  * This file created by Irfan Khan <ikhan@qualcomm.com>
10  * Copyright (c) 2001  by QUALCOMM, Incorporated.
11  * All Rights reserved.
12  *
13  * Routines to compress and uncompress TCP packets (for transmission
14  * over low speed serial lines).
15  *
16  * Copyright (c) 1989 Regents of the University of California.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that the above copyright notice and this paragraph are
21  * duplicated in all such forms and that any documentation,
22  * advertising materials, and other materials related to such
23  * distribution and use acknowledge that the software was developed
24  * by the University of California, Berkeley.  The name of the
25  * University may not be used to endorse or promote products derived
26  * from this software without specific prior written permission.
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
28  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
29  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  *      Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
32  *      - Initial distribution.
33  *
34  *
35  * modified for KA9Q Internet Software Package by
36  * Katie Stevens (dkstevens@ucdavis.edu)
37  * University of California, Davis
38  * Computing Services
39  *      - 01-31-90      initial adaptation (from 1.19)
40  *      PPP.05  02-15-90 [ks]
41  *      PPP.08  05-02-90 [ks]   use PPP protocol field to signal compression
42  *      PPP.15  09-90    [ks]   improve mbuf handling
43  *      PPP.16  11-02    [karn] substantially rewritten to use NOS facilities
44  *
45  *      - Feb 1991      Bill_Simpson@um.cc.umich.edu
46  *                      variable number of conversation slots
47  *                      allow zero or one slots
48  *                      separate routines
49  *                      status display
50  *      - Jul 1994      Dmitry Gorodchanin
51  *                      Fixes for memory leaks.
52  *      - Oct 1994      Dmitry Gorodchanin
53  *                      Modularization.
54  *      - Jan 1995      Bjorn Ekwall
55  *                      Use ip_fast_csum from ip.h
56  *      - July 1995     Christos A. Polyzols
57  *                      Spotted bug in tcp option checking
58  *      - Sep 2001      Irfan Khan
59  *                      Rewrite to make the code work for ethereal.
60  */
61
62 #ifdef HAVE_CONFIG_H
63 # include "config.h"
64 #endif
65
66 #include <glib.h>
67 #include <string.h>
68 #include <epan/packet.h>
69 #include <epan/prefs.h>
70 #include "packet-ppp.h"
71 #include <epan/ppptypes.h>
72 #include <epan/ipproto.h>
73 #include <epan/in_cksum.h>
74 #include <epan/emem.h>
75
76 /* Define relevant IP/TCP parameters */
77 #define IP_FIELD_TOT_LEN      2 /* Total length field in IP hdr           */
78 #define IP_FIELD_PROTOCOL     9 /* Protocol field byte in IP hdr          */
79 #define IP_ADDR_SIZE          4 /* Size in bytes of IPv4 address          */
80 #define IP_FIELD_SRC         12 /* Byte 12 in IP hdr - src address        */
81 #define IP_FIELD_DST         16 /* Byte 16 in IP hdr - dst address        */
82 #define IP_HDR_LEN           20 /* Minimum IP header length               */
83 #define IP_HDR_LEN_MASK    0x0f /* Mask for header length field           */
84 #define IP_MAX_OPT_LEN       44 /* Max length of IP options               */
85 #define TCP_FIELD_HDR_LEN    12 /* Data offset field in TCP hdr           */
86 #define TCP_HDR_LEN          20 /* Minimum TCP header length              */
87 #define TCP_MAX_OPT_LEN      44 /* Max length of TCP options              */
88 #define TCP_SIMUL_CONV_MAX  256 /* Max number of simul. TCP conversations */
89 #define TCP_PUSH_BIT       0x08 /* TCP push bit                           */
90 #define TCP_URG_BIT        0x20 /* TCP urgent bit                         */
91
92 /* Bits in first octet of compressed packet */
93 /* flag bits for what changed in a packet */
94 #define NEW_C           0x40 /* Connection number changed               */
95 #define NEW_I           0x20 /* IP sequence number change by value != 1 */
96 #define CHANGE_PUSH_BIT 0x10 /* TCP push bit set                        */
97 #define NEW_S           0x08 /* Sequence number changed                 */
98 #define NEW_A           0x04 /* Ack sequence number changed             */
99 #define NEW_W           0x02 /* Window changed                          */
100 #define NEW_U           0x01 /* Urgent pointer present                  */
101
102 /* reserved, special-case values of above */
103 #define SPECIAL_I     (NEW_S|NEW_W|NEW_U)    /* echoed interactive traffic */
104 #define SPECIAL_D     (NEW_S|NEW_A|NEW_W|NEW_U)/* unidirectional data */
105 #define SPECIALS_MASK (NEW_S|NEW_A|NEW_W|NEW_U)
106
107 /* Function return values */
108 #define VJ_OK           0
109 #define VJ_ERROR       -1
110
111 /* Define for 0 */
112 #define ZERO            0
113
114 /* VJ Mem Chunk defines */
115 #define VJ_DATA_SIZE  128 /* Max IP hdr(64)+Max TCP hdr(64) */
116
117 /* IP and TCP header types */
118 typedef struct {
119   guint8  ihl_version;
120   guint8  tos;
121   guint16 tot_len;
122   guint16 id;
123   guint16 frag_off;
124   guint8  ttl;
125   guint8  proto;
126   guint16 cksum;
127   guint32 src;
128   guint32 dst;
129 } iphdr_type;
130
131 typedef struct {
132   guint16 srcport;
133   guint16 dstport;
134   guint32 seq;
135   guint32 ack_seq;
136   guint8  off_x2;
137   guint8  flags;
138   guint16 window;
139   guint16 cksum;
140   guint16 urg_ptr;
141 } tcphdr_type;
142
143 #define TCP_OFFSET(th)  (((th)->off_x2 & 0xf0) >> 4)
144
145 /* State per active tcp conversation */
146 typedef struct cstate {
147   iphdr_type cs_ip;
148   tcphdr_type cs_tcp;
149   guint8 cs_ipopt[IP_MAX_OPT_LEN];
150   guint8 cs_tcpopt[TCP_MAX_OPT_LEN];
151   guint32 flags;
152 #define SLF_TOSS  0x00000001    /* tossing rcvd frames until id received */
153 } cstate;
154
155 /* All the state data for one serial line */
156 typedef struct {
157   cstate rstate[TCP_SIMUL_CONV_MAX]; /* receive connection states (array) */
158   guint8 recv_current;               /* most recent rcvd id */
159 } slcompress;
160
161 /* Initialize the protocol and registered fields */
162 static int proto_vj = -1;
163
164 static int hf_vj_change_mask = -1;
165 static int hf_vj_change_mask_c = -1;
166 static int hf_vj_change_mask_i = -1;
167 static int hf_vj_change_mask_p = -1;
168 static int hf_vj_change_mask_s = -1;
169 static int hf_vj_change_mask_a = -1;
170 static int hf_vj_change_mask_w = -1;
171 static int hf_vj_change_mask_u = -1;
172 static int hf_vj_connection_number = -1;
173 static int hf_vj_tcp_cksum = -1;
174 static int hf_vj_urp = -1;
175 static int hf_vj_win_delta = -1;
176 static int hf_vj_ack_delta = -1;
177 static int hf_vj_seq_delta = -1;
178 static int hf_vj_ip_id_delta = -1;
179
180 static gint ett_vj = -1;
181 static gint ett_vj_changes = -1;
182
183 /* Protocol handles */
184 static dissector_handle_t ip_handle;
185 static dissector_handle_t data_handle;
186
187 /* State repository (Full Duplex) */
188 #define RX_TX_STATE_COUNT 2
189 static slcompress *rx_tx_state[RX_TX_STATE_COUNT] = {NULL, NULL};
190
191 /* Mem Chunks for storing decompressed headers */
192 typedef struct {
193         int     offset;                 /* uppermost bit is "can't dissect" flag */
194         guint8  data[VJ_DATA_SIZE];
195 } vj_header_t;
196
197 /* Function prototypes */
198 static int get_unsigned_delta(tvbuff_t *tvb, int *offsetp, int hf,
199                         proto_tree *tree);
200 static int get_signed_delta(tvbuff_t *tvb, int *offsetp, int hf,
201                         proto_tree *tree);
202 static guint16 ip_csum(const guint8 *ptr, guint32 len);
203 static slcompress *slhc_init(void);
204 static void vj_init(void);
205 static gint vjc_process(tvbuff_t *src_tvb, packet_info *pinfo, proto_tree *tree,
206                       slcompress *comp);
207 static gint vjc_tvb_setup(tvbuff_t *src_tvb, tvbuff_t **dst_tvb,
208                           packet_info *pinfo);
209
210 /* Dissector for VJ Uncompressed packets */
211 static void
212 dissect_vjuc(tvbuff_t *tvb, packet_info *pinfo, proto_tree * tree)
213 {
214   proto_item *ti;
215   proto_tree *vj_tree     = NULL;
216   slcompress *comp;
217   int         i;
218   gint        conn_index;
219   cstate     *cs          = NULL;
220   guint8      ihl;
221   guint8      thl;
222   guint8     *buffer;
223   tvbuff_t   *next_tvb;
224   gint        isize       = tvb_length(tvb);
225   gint        ipsize;
226
227   if(check_col(pinfo->cinfo, COL_PROTOCOL))
228     col_set_str(pinfo->cinfo, COL_INFO, "PPP VJ");
229
230   if(tree != NULL) {
231     ti = proto_tree_add_protocol_format(tree, proto_vj, tvb, 0, -1,
232                                         "PPP VJ Compression: Uncompressed data");
233     vj_tree = proto_item_add_subtree(ti, ett_vj);
234   }
235
236   if(pinfo->p2p_dir == P2P_DIR_UNKNOWN) {
237     /* Direction of the traffic unknown - can't update state */
238     comp = NULL;
239   } else {
240     /* Get state for that direction */
241     comp = rx_tx_state[pinfo->p2p_dir];
242   }
243
244   /*
245    * Check to make sure we can fetch the connection index.
246    */
247   if(!tvb_bytes_exist(tvb, IP_FIELD_PROTOCOL, 1)) {
248     /*
249      * We don't.  We can't even mark a connection as non-decompressable,
250      * as we don't know which connection this is.  Mark them all as
251      * non-decompressable.
252      */
253     if(check_col(pinfo->cinfo, COL_INFO))
254       col_set_str(pinfo->cinfo, COL_INFO, "VJ uncompressed TCP (not enough data available)");
255     if(tree != NULL)
256       call_dissector(data_handle, tvb, pinfo, tree);
257     if(comp != NULL) {
258       for(i = 0; i < TCP_SIMUL_CONV_MAX; i++)
259         comp->rstate[i].flags |= SLF_TOSS;
260     }
261     return;
262   }
263
264   /* Get connection index */
265   conn_index = tvb_get_guint8(tvb, IP_FIELD_PROTOCOL);
266   if(tree != NULL)
267     proto_tree_add_uint(vj_tree, hf_vj_connection_number, tvb,
268                         IP_FIELD_PROTOCOL, 1, conn_index);
269
270   /*
271    * Update the current connection, and get a pointer to its state.
272    */
273   if(comp != NULL) {
274     comp->recv_current = conn_index;
275     cs = &comp->rstate[conn_index];
276   }
277
278   /* Get the IP header length */
279   ihl = tvb_get_guint8(tvb, 0) & IP_HDR_LEN_MASK;
280   ihl <<= 2;
281
282   /* Check IP header length */
283   if(ihl < IP_HDR_LEN) {
284     if(check_col(pinfo->cinfo, COL_INFO)) {
285       col_add_fstr(pinfo->cinfo, COL_INFO, "VJ uncompressed TCP (IP header length (%u) < %u)",
286                    ihl, IP_HDR_LEN);
287     }
288     if(cs != NULL)
289       cs->flags |= SLF_TOSS;
290     return;
291   }
292
293   /* Make sure we have the full IP header */
294   if(isize < ihl) {
295     if(check_col(pinfo->cinfo, COL_INFO))
296       col_set_str(pinfo->cinfo, COL_INFO, "VJ uncompressed TCP (not enough data available)");
297     if(tree != NULL)
298       call_dissector(data_handle, tvb, pinfo, tree);
299     if(cs != NULL)
300       cs->flags |= SLF_TOSS;
301     return;
302   }
303
304   if(check_col(pinfo->cinfo, COL_INFO))
305     col_set_str(pinfo->cinfo, COL_INFO, "VJ uncompressed TCP");
306
307   /*
308    * Copy packet data to a buffer, and replace the connection index with
309    * the protocol type (which is always TCP), to give the actual IP header.
310    */
311   buffer = tvb_memdup(tvb, 0, isize);
312   buffer[IP_FIELD_PROTOCOL] = IP_PROTO_TCP;
313
314   /* Check IP checksum */
315   if(ip_csum(buffer, ihl) != ZERO) {
316     /*
317      * Checksum invalid - don't update state, and don't decompress
318      * any subsequent compressed packets in this direction.
319      */
320     if(cs != NULL)
321       cs->flags |= SLF_TOSS;
322     cs = NULL;  /* disable state updates */
323   } else {
324     /* Do we have the TCP header length in the tvbuff? */
325     if(!tvb_bytes_exist(tvb, ihl + TCP_FIELD_HDR_LEN, 1)) {
326       /* We don't, so we can't provide enough data for decompression */
327       if(check_col(pinfo->cinfo, COL_INFO))
328         col_set_str(pinfo->cinfo, COL_INFO, "VJ uncompressed TCP (not enough data available)");
329       if(cs != NULL)
330         cs->flags |= SLF_TOSS;
331       cs = NULL;  /* disable state updates */
332     } else {
333       /* Get the TCP header length */
334       thl = tvb_get_guint8(tvb, ihl + TCP_FIELD_HDR_LEN);
335       thl = ((thl & 0xf0) >> 4) * 4;
336
337       /* Check TCP header length */
338       if(thl < TCP_HDR_LEN) {
339         if(check_col(pinfo->cinfo, COL_INFO)) {
340           col_add_fstr(pinfo->cinfo, COL_INFO, "VJ uncompressed TCP (TCP header length (%u) < %u)",
341                        thl, TCP_HDR_LEN);
342         }
343         if(cs != NULL)
344           cs->flags |= SLF_TOSS;
345         cs = NULL;  /* disable state updates */
346       } else {
347         /* Make sure we have the full TCP header */
348         if(isize < thl) {
349           if(check_col(pinfo->cinfo, COL_INFO))
350             col_set_str(pinfo->cinfo, COL_INFO, "VJ uncompressed TCP (not enough data available)");
351           if(cs != NULL)
352             cs->flags |= SLF_TOSS;
353           cs = NULL;  /* disable state updates */
354         }
355       }
356     }
357   }
358
359   /*
360    * If packet seen for first time, update state if we have state and can
361    * update it.
362    */
363   if(!pinfo->fd->flags.visited) {
364     if(cs != NULL) {
365       cs->flags &= ~SLF_TOSS;
366       memcpy(&cs->cs_ip, &buffer[0], IP_HDR_LEN);
367       memcpy(&cs->cs_tcp, &buffer[ihl], TCP_HDR_LEN);
368       if(ihl > IP_HDR_LEN)
369         memcpy(cs->cs_ipopt, &buffer[sizeof(iphdr_type)], ihl - IP_HDR_LEN);
370       if(TCP_OFFSET(&(cs->cs_tcp)) > 5)
371         memcpy(cs->cs_tcpopt, &buffer[ihl + sizeof(tcphdr_type)],
372                    (TCP_OFFSET(&(cs->cs_tcp)) - 5) * 4);
373     }
374   }
375
376   /*
377    * Set up tvbuff containing packet with protocol type.
378    * Neither header checksum is recalculated.
379    *
380    * Use the length field from the IP header as the reported length;
381    * use the minimum of that and the number of bytes we got from
382    * the tvbuff as the actual length, just in case the tvbuff we were
383    * handed includes part or all of the FCS (because the FCS preference
384    * for the PPP dissector doesn't match the FCS size in this session).
385    */
386   ipsize = pntohs(&buffer[IP_FIELD_TOT_LEN]);
387   if (ipsize < isize)
388     isize = ipsize;
389   next_tvb = tvb_new_real_data(buffer, isize, ipsize);
390   tvb_set_child_real_data_tvbuff(tvb, next_tvb);
391   add_new_data_source(pinfo, next_tvb, "VJ Uncompressed");
392
393   /*
394    * Call IP dissector.
395    */
396   call_dissector(ip_handle, next_tvb, pinfo, tree);
397 }
398
399 /* Dissector for VJ Compressed packets */
400 static void
401 dissect_vjc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
402 {
403   proto_item *ti;
404   proto_tree *vj_tree     = NULL;
405   tvbuff_t   *next_tvb = NULL;
406   slcompress *comp     = NULL;
407   gint        err      = VJ_ERROR;
408
409   if(check_col(pinfo->cinfo, COL_PROTOCOL))
410     col_set_str(pinfo->cinfo, COL_INFO, "PPP VJ");
411
412   if(tree != NULL) {
413     ti = proto_tree_add_protocol_format(tree, proto_vj, tvb, 0, -1,
414                                         "PPP VJ Compression: Compressed data");
415     vj_tree = proto_item_add_subtree(ti, ett_vj);
416   }
417
418   if(!ppp_vj_decomp || pinfo->p2p_dir == P2P_DIR_UNKNOWN) {
419     /*
420      * VJ decompression turned off, so we shouldn't decompress, or
421      * direction of the traffic unknown, so we can't decompress.
422      */
423     comp = NULL;
424   } else {
425     /* Get state for that direction */
426     comp = rx_tx_state[pinfo->p2p_dir];
427   }
428
429   /* Process the compressed data header */
430   if(vjc_process(tvb, pinfo, vj_tree, comp) == VJ_ERROR)
431     return;
432
433   /* Decompression possible - set up tvb containing decompressed packet */
434   err = vjc_tvb_setup(tvb, &next_tvb, pinfo);
435   if(err == VJ_ERROR) {
436     if(tree != NULL)
437       call_dissector(data_handle, tvb, pinfo, vj_tree);
438     return;
439   }
440
441   /* No errors, so call IP dissector */
442   call_dissector(ip_handle, next_tvb, pinfo, tree);
443 }
444
445 /* Registration functions for dissectors */
446 void
447 proto_register_vj(void)
448 {
449   static hf_register_info hf[] = {
450     { &hf_vj_change_mask,
451       { "Change mask",  "vj.change_mask",       FT_UINT8, BASE_HEX,
452         NULL, 0x0, "", HFILL }},
453     { &hf_vj_change_mask_c,
454       { "Connection changed",           "vj.change_mask_c",     FT_BOOLEAN, 8,
455         NULL, NEW_C, "Connection number changed", HFILL }},
456     { &hf_vj_change_mask_i,
457       { "IP ID change != 1",            "vj.change_mask_i",     FT_BOOLEAN, 8,
458         NULL, NEW_I, "IP ID changed by a value other than 1", HFILL }},
459     { &hf_vj_change_mask_p,
460       { "Push bit set",                 "vj.change_mask_p",     FT_BOOLEAN, 8,
461         NULL, CHANGE_PUSH_BIT, "TCP PSH flag set", HFILL }},
462     { &hf_vj_change_mask_s,
463       { "Sequence number changed",      "vj.change_mask_s",     FT_BOOLEAN, 8,
464         NULL, NEW_S, "Sequence number changed", HFILL }},
465     { &hf_vj_change_mask_a,
466       { "Ack number changed",           "vj.change_mask_a",     FT_BOOLEAN, 8,
467         NULL, NEW_A, "Acknowledgement sequence number changed", HFILL }},
468     { &hf_vj_change_mask_w,
469       { "Window changed",               "vj.change_mask_w",     FT_BOOLEAN, 8,
470         NULL, NEW_W, "TCP window changed", HFILL }},
471     { &hf_vj_change_mask_u,
472       { "Urgent pointer set",           "vj.change_mask_u",     FT_BOOLEAN, 8,
473         NULL, NEW_U, "Urgent pointer set", HFILL }},
474     { &hf_vj_connection_number,
475       { "Connection number",    "vj.connection_number", FT_UINT8, BASE_DEC,
476         NULL, 0x0, "Connection number", HFILL }},
477     { &hf_vj_tcp_cksum,
478       { "TCP checksum",                 "vj.tcp_cksum", FT_UINT16, BASE_HEX,
479         NULL, 0x0, "TCP checksum", HFILL }},
480     { &hf_vj_urp,
481       { "Urgent pointer",               "vj.urp",       FT_UINT16, BASE_DEC,
482         NULL, 0x0, "Urgent pointer", HFILL }},
483     { &hf_vj_win_delta,
484       { "Window delta",                 "vj.win_delta", FT_INT16, BASE_DEC,
485         NULL, 0x0, "Delta for window", HFILL }},
486     { &hf_vj_ack_delta,
487       { "Ack delta",                    "vj.ack_delta", FT_UINT16, BASE_DEC,
488         NULL, 0x0, "Delta for acknowledgment sequence number", HFILL }},
489     { &hf_vj_seq_delta,
490       { "Sequence delta",               "vj.seq_delta", FT_UINT16, BASE_DEC,
491         NULL, 0x0, "Delta for sequence number", HFILL }},
492     { &hf_vj_ip_id_delta,
493       { "IP ID delta",                  "vj.ip_id_delta",       FT_UINT16, BASE_DEC,
494         NULL, 0x0, "Delta for IP ID", HFILL }},
495   };
496   static gint *ett[] = {
497     &ett_vj,
498     &ett_vj_changes,
499   };
500
501   proto_vj = proto_register_protocol("PPP VJ Compression", "PPP VJ", "vj");
502   proto_register_field_array(proto_vj, hf, array_length(hf));
503   proto_register_subtree_array(ett, array_length(ett));
504   register_init_routine(&vj_init);
505 }
506
507 void
508 proto_reg_handoff_vj(void)
509 {
510   dissector_handle_t vjc_handle;
511   dissector_handle_t vjuc_handle;
512
513   vjc_handle = create_dissector_handle(dissect_vjc, proto_vj);
514   dissector_add("ppp.protocol", PPP_VJC_COMP, vjc_handle);
515
516   vjuc_handle = create_dissector_handle(dissect_vjuc, proto_vj);
517   dissector_add("ppp.protocol", PPP_VJC_UNCOMP, vjuc_handle);
518
519   ip_handle = find_dissector("ip");
520   data_handle = find_dissector("data");
521 }
522
523 /* Initialization function */
524 static void
525 vj_init(void)
526 {
527   gint i           = ZERO;
528
529   for(i = 0; i < RX_TX_STATE_COUNT; i++) {
530     rx_tx_state[i] = slhc_init();
531   }
532   return;
533 }
534
535 /* Initialization routine for VJ decompression */
536 static slcompress *
537 slhc_init(void)
538 {
539   slcompress *comp = se_alloc(sizeof(slcompress));
540   int         i;
541
542   memset(comp, ZERO, sizeof(slcompress));
543
544   /*
545    * Initialize the state; there is no current connection, and
546    * we have no header data for any of the connections, as we
547    * haven't yet seen an uncompressed frame.
548    */
549   comp->recv_current = TCP_SIMUL_CONV_MAX - 1;
550   for (i = 0; i < TCP_SIMUL_CONV_MAX; i++)
551     comp->rstate[i].flags |= SLF_TOSS;
552   return comp;
553 }
554
555 /* Setup the decompressed packet tvb for VJ compressed packets */
556 static gint
557 vjc_tvb_setup(tvbuff_t *src_tvb,
558               tvbuff_t **dst_tvb,
559               packet_info *pinfo)
560 {
561   vj_header_t *hdr_buf;
562   guint8       offset;
563   guint8      *data_ptr;
564   iphdr_type  *ip;
565   tcphdr_type *thp;
566   gint         hdr_len;
567   gint         buf_len;
568   guint8      *pbuf;
569
570   DISSECTOR_ASSERT(src_tvb);
571
572   /* Get decompressed header stored in fd protocol area */
573   hdr_buf = p_get_proto_data(pinfo->fd, proto_vj);
574   if(hdr_buf == NULL) {
575     if(check_col(pinfo->cinfo, COL_INFO))
576       col_set_str(pinfo->cinfo, COL_INFO, "VJ compressed TCP (previous data bad or missing)");
577     return VJ_ERROR;
578   }
579
580   if(check_col(pinfo->cinfo, COL_INFO))
581     col_set_str(pinfo->cinfo, COL_INFO, "VJ compressed TCP");
582
583   /* Get the data offset in the tvbuff */
584   offset  = hdr_buf->offset;
585
586   /* Copy header and form tvb */
587   data_ptr = hdr_buf->data;
588   ip = (iphdr_type *)data_ptr;
589   hdr_len  = lo_nibble(ip->ihl_version) * 4;
590   thp = (tcphdr_type *)(data_ptr + hdr_len);
591   hdr_len += TCP_OFFSET(thp) * 4;
592   buf_len  = tvb_length(src_tvb) + hdr_len - offset;
593   pbuf     = g_malloc(buf_len);
594   memcpy(pbuf, data_ptr, hdr_len);
595   tvb_memcpy(src_tvb, pbuf + hdr_len, offset, buf_len - hdr_len);
596   *dst_tvb = tvb_new_real_data(pbuf, buf_len, g_ntohs(ip->tot_len));
597   tvb_set_child_real_data_tvbuff(src_tvb, *dst_tvb);
598   add_new_data_source(pinfo, *dst_tvb, "VJ Decompressed");
599   return VJ_OK;
600 }
601
602 /*
603  * For VJ compressed packet:
604  *
605  *      check if it is malformed;
606  *      dissect the relevant fields;
607  *      update the decompressor state on the first pass.
608  */
609 static gint
610 vjc_process(tvbuff_t *src_tvb, packet_info *pinfo, proto_tree *tree,
611             slcompress *comp)
612 {
613   int            offset     = ZERO;
614   int            i;
615   gint           changes;
616   proto_item    *ti;
617   proto_tree    *changes_tree;
618   guint8         conn_index;
619   cstate        *cs         = NULL;
620   iphdr_type    *ip         = NULL;
621   tcphdr_type   *thp        = NULL;
622   guint16        tcp_cksum;
623   gint           hdrlen     = ZERO;
624   guint16        word;
625   int            delta;
626   gint           len;
627   vj_header_t   *buf_hdr;
628   guint8        *data_ptr;
629
630   if(tvb_length(src_tvb) < 3){
631     /*
632      * We don't even have enough data for the change byte, so we can't
633      * determine which connection this is; mark all connections as
634      * non-decompressible.
635      */
636     if(check_col(pinfo->cinfo, COL_INFO))
637       col_set_str(pinfo->cinfo, COL_INFO, "VJ compressed TCP (not enough data available)");
638     if(tree != NULL)
639       call_dissector(data_handle, src_tvb, pinfo, tree);
640     if(comp != NULL) {
641       for(i = 0; i < TCP_SIMUL_CONV_MAX; i++)
642         comp->rstate[i].flags |= SLF_TOSS;
643     }
644     return VJ_ERROR;
645   }
646
647   /* Read the change byte */
648   changes = tvb_get_guint8(src_tvb, offset);
649   if(tree != NULL) {
650     switch (changes & SPECIALS_MASK) {
651
652     case SPECIAL_I:
653       ti = proto_tree_add_uint_format(tree, hf_vj_change_mask, src_tvb,
654                                       offset, 1, changes,
655                                       "Change mask: 0x%02x (echoed interactive traffic)",
656                                       changes);
657       break;
658
659     case SPECIAL_D:
660       ti = proto_tree_add_uint_format(tree, hf_vj_change_mask, src_tvb,
661                                       offset, 1, changes,
662                                       "Change mask: 0x%02x (unidirectional data)",
663                                       changes);
664       break;
665
666     default:
667       /*
668        * XXX - summarize bits?
669        */
670       ti = proto_tree_add_uint_format(tree, hf_vj_change_mask, src_tvb,
671                                       offset, 1, changes,
672                                       "Change mask: 0x%02x", changes);
673       break;
674     }
675     changes_tree = proto_item_add_subtree(ti, ett_vj_changes);
676     proto_tree_add_boolean(changes_tree, hf_vj_change_mask_c, src_tvb,
677                            offset, 1, changes);
678     proto_tree_add_boolean(changes_tree, hf_vj_change_mask_i, src_tvb,
679                            offset, 1, changes);
680     proto_tree_add_boolean(changes_tree, hf_vj_change_mask_p, src_tvb,
681                            offset, 1, changes);
682     proto_tree_add_boolean(changes_tree, hf_vj_change_mask_s, src_tvb,
683                            offset, 1, changes);
684     proto_tree_add_boolean(changes_tree, hf_vj_change_mask_a, src_tvb,
685                            offset, 1, changes);
686     proto_tree_add_boolean(changes_tree, hf_vj_change_mask_w, src_tvb,
687                            offset, 1, changes);
688     proto_tree_add_boolean(changes_tree, hf_vj_change_mask_u, src_tvb,
689                            offset, 1, changes);
690   }
691   offset++;
692
693   if(changes & NEW_C){    /* Read conn index */
694     conn_index = tvb_get_guint8(src_tvb, offset);
695     if(tree != NULL)
696       proto_tree_add_uint(tree, hf_vj_connection_number, src_tvb, offset, 1,
697                           conn_index);
698     offset++;
699     if(comp != NULL)
700       comp->recv_current = conn_index;
701   }
702
703   if(!pinfo->fd->flags.visited) {
704     /*
705      * This is the first time this frame has been seen, so we need
706      * state information to decompress it.  If that information isn't
707      * available, don't use the state information, and don't update it,
708      * either.
709      */
710     if(comp != NULL && !(comp->rstate[comp->recv_current].flags & SLF_TOSS)) {
711       cs = &comp->rstate[comp->recv_current];
712       thp = &cs->cs_tcp;
713       ip = &cs->cs_ip;
714     }
715   }
716
717   /* Build TCP and IP headers */
718   tcp_cksum = tvb_get_ntohs(src_tvb, offset);
719   if(tree != NULL)
720     proto_tree_add_uint(tree, hf_vj_tcp_cksum, src_tvb, offset, 2, tcp_cksum);
721   if(cs != NULL) {
722     hdrlen = lo_nibble(ip->ihl_version) * 4 + TCP_OFFSET(thp) * 4;
723     thp->cksum = g_htons(tcp_cksum);
724   }
725   offset += 2;
726   if(cs != NULL) {
727     if(changes & CHANGE_PUSH_BIT)
728       thp->flags |= TCP_PUSH_BIT;
729     else
730       thp->flags &= ~TCP_PUSH_BIT;
731   }
732
733   /* Deal with special cases and normal deltas */
734   switch(changes & SPECIALS_MASK){
735     case SPECIAL_I:                   /* Echoed terminal traffic */
736       if(cs != NULL) {
737         word = g_ntohs(ip->tot_len) - hdrlen;
738         thp->ack_seq = g_htonl(g_ntohl(thp->ack_seq) + word);
739         thp->seq = g_htonl(g_ntohl(thp->seq) + word);
740       }
741       break;
742     case SPECIAL_D:                   /* Unidirectional data */
743       if(cs != NULL)
744         thp->seq = g_htonl(g_ntohl(thp->seq) + g_ntohs(ip->tot_len) - hdrlen);
745       break;
746     default:
747       if(changes & NEW_U){
748         delta = get_unsigned_delta(src_tvb, &offset, hf_vj_urp, tree);
749         if(cs != NULL) {
750           thp->urg_ptr = delta;
751           thp->flags |= TCP_URG_BIT;
752         }
753       } else {
754         if(cs != NULL)
755           thp->flags &= ~TCP_URG_BIT;
756       }
757       if(changes & NEW_W) {
758         delta = get_signed_delta(src_tvb, &offset, hf_vj_win_delta, tree);
759         if(cs != NULL)
760           thp->window = g_htons(g_ntohs(thp->window) + delta);
761       }
762       if(changes & NEW_A) {
763         delta = get_unsigned_delta(src_tvb, &offset, hf_vj_ack_delta, tree);
764         if(cs != NULL)
765           thp->ack_seq = g_htonl(g_ntohl(thp->ack_seq) + delta);
766       }
767       if(changes & NEW_S) {
768         delta = get_unsigned_delta(src_tvb, &offset, hf_vj_seq_delta, tree);
769         if(cs != NULL)
770           thp->seq = g_htonl(g_ntohl(thp->seq) + delta);
771       }
772       break;
773   }
774   if(changes & NEW_I)
775     delta = get_unsigned_delta(src_tvb, &offset, hf_vj_ip_id_delta, tree);
776   else
777     delta = 1;
778   if(cs != NULL)
779     ip->id = g_htons(g_ntohs(ip->id) + delta);
780
781   /* Compute IP packet length and the buffer length needed */
782   len = tvb_reported_length_remaining(src_tvb, offset);
783   if(len < ZERO) {
784     /*
785      * This shouldn't happen, as we *were* able to fetch stuff right before
786      * offset.
787      */
788     if(check_col(pinfo->cinfo, COL_INFO))
789       col_set_str(pinfo->cinfo, COL_INFO, "VJ compressed TCP (not enough data available)");
790     if(cs != NULL)
791       cs->flags |= SLF_TOSS;
792     return VJ_ERROR;
793   }
794
795   /* Show the TCP payload */
796   if(tree != NULL && tvb_offset_exists(src_tvb, offset))
797     proto_tree_add_text(tree, src_tvb, offset, -1, "TCP payload");
798
799   /* Nothing more to do if we don't have any compression state */
800   if(comp == NULL) {
801     /* Direction of the traffic unknown - can't decompress */
802     if(check_col(pinfo->cinfo, COL_INFO))
803       col_set_str(pinfo->cinfo, COL_INFO, "VJ compressed TCP (direction unknown)");
804     return VJ_ERROR;
805   }
806
807   if((cs != NULL) && (!pinfo->fd->flags.visited)) {
808     len += hdrlen;
809     ip->tot_len = g_htons(len);
810     /* Compute IP check sum */
811     ip->cksum = ZERO;
812     ip->cksum = ip_csum((guint8 *)ip, lo_nibble(ip->ihl_version) * 4);
813
814     /* Store the reconstructed header in frame data area */
815     buf_hdr = se_alloc(sizeof (vj_header_t));
816     buf_hdr->offset = offset;  /* Offset in tvbuff is also stored */
817     data_ptr = buf_hdr->data;
818     memcpy(data_ptr, ip, IP_HDR_LEN);
819     data_ptr += IP_HDR_LEN;
820     if(lo_nibble(ip->ihl_version) > 5) {
821       memcpy(data_ptr, cs->cs_ipopt, (lo_nibble(ip->ihl_version) - 5) * 4);
822       data_ptr += (lo_nibble(ip->ihl_version) - 5) * 4;
823     }
824     memcpy(data_ptr, thp, TCP_HDR_LEN);
825     data_ptr += TCP_HDR_LEN;
826     if(TCP_OFFSET(thp) > 5)
827       memcpy(data_ptr, cs->cs_tcpopt, (TCP_OFFSET(thp) - 5) * 4);
828     p_add_proto_data(pinfo->fd, proto_vj, buf_hdr);
829   }
830
831   return VJ_OK;
832 }
833
834 /*
835  * Get an unsigned delta for a field, and put it into the protocol tree if
836  * we're building a protocol tree.
837  */
838 static int
839 get_unsigned_delta(tvbuff_t *tvb, int *offsetp, int hf, proto_tree *tree)
840 {
841   int offset = *offsetp;
842   int len;
843   guint16 del;
844
845   len = 1;
846   del = tvb_get_guint8(tvb, offset++);
847   if(del == ZERO){
848     del = tvb_get_ntohs(tvb, offset);
849     offset += 2;
850     len += 2;
851   }
852   if(tree != NULL)
853     proto_tree_add_uint(tree, hf, tvb, *offsetp, len, del);
854   *offsetp = offset;
855   return del;
856 }
857
858 /*
859  * Get a signed delta for a field, and put it into the protocol tree if
860  * we're building a protocol tree.
861  */
862 static int
863 get_signed_delta(tvbuff_t *tvb, int *offsetp, int hf, proto_tree *tree)
864 {
865   int offset = *offsetp;
866   int len;
867   gint16 del;
868
869   len = 1;
870   del = tvb_get_guint8(tvb, offset++);
871   if(del == ZERO){
872     del = tvb_get_ntohs(tvb, offset);
873     offset += 2;
874     len += 2;
875   }
876   if(tree != NULL)
877     proto_tree_add_int(tree, hf, tvb, *offsetp, len, del);
878   *offsetp = offset;
879   return del;
880 }
881
882 /* Wrapper for in_cksum function */
883 static guint16
884 ip_csum(const guint8 * ptr, guint32 len)
885 {
886   vec_t cksum_vec[1];
887
888   cksum_vec[0].ptr = ptr;
889   cksum_vec[0].len = len;
890   return in_cksum(&cksum_vec[0], 1);
891 }