Don't guard col_set_str (COL_PROTOCOL) with col_check
[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  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
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 wireshark.
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 gint vjc_process(tvbuff_t *src_tvb, packet_info *pinfo, proto_tree *tree,
205                       slcompress *comp);
206 static gint vjc_tvb_setup(tvbuff_t *src_tvb, tvbuff_t **dst_tvb,
207                           packet_info *pinfo);
208
209 /* Dissector for VJ Uncompressed packets */
210 static void
211 dissect_vjuc(tvbuff_t *tvb, packet_info *pinfo, proto_tree * tree)
212 {
213   proto_item *ti;
214   proto_tree *vj_tree     = NULL;
215   slcompress *comp;
216   int         i;
217   gint        conn_index;
218   cstate     *cs          = NULL;
219   guint8      ihl;
220   guint8      thl;
221   guint8     *buffer;
222   tvbuff_t   *next_tvb;
223   gint        isize       = tvb_length(tvb);
224   gint        ipsize;
225
226   col_set_str(pinfo->cinfo, COL_PROTOCOL, "PPP VJ");
227
228   if(tree != NULL) {
229     ti = proto_tree_add_protocol_format(tree, proto_vj, tvb, 0, -1,
230                                         "PPP VJ Compression: Uncompressed data");
231     vj_tree = proto_item_add_subtree(ti, ett_vj);
232   }
233
234   if(pinfo->p2p_dir == P2P_DIR_UNKNOWN) {
235     /* Direction of the traffic unknown - can't update state */
236     comp = NULL;
237   } else {
238     /* Get state for that direction */
239     comp = rx_tx_state[pinfo->p2p_dir];
240   }
241
242   /*
243    * Check to make sure we can fetch the connection index.
244    */
245   if(!tvb_bytes_exist(tvb, IP_FIELD_PROTOCOL, 1)) {
246     /*
247      * We don't.  We can't even mark a connection as non-decompressable,
248      * as we don't know which connection this is.  Mark them all as
249      * non-decompressable.
250      */
251     if(check_col(pinfo->cinfo, COL_INFO))
252       col_set_str(pinfo->cinfo, COL_INFO, "VJ uncompressed TCP (not enough data available)");
253     if(tree != NULL)
254       call_dissector(data_handle, tvb, pinfo, tree);
255     if(comp != NULL) {
256       for(i = 0; i < TCP_SIMUL_CONV_MAX; i++)
257         comp->rstate[i].flags |= SLF_TOSS;
258     }
259     return;
260   }
261
262   /* Get connection index */
263   conn_index = tvb_get_guint8(tvb, IP_FIELD_PROTOCOL);
264   if(tree != NULL)
265     proto_tree_add_uint(vj_tree, hf_vj_connection_number, tvb,
266                         IP_FIELD_PROTOCOL, 1, conn_index);
267
268   /*
269    * Update the current connection, and get a pointer to its state.
270    */
271   if(comp != NULL) {
272     comp->recv_current = conn_index;
273     cs = &comp->rstate[conn_index];
274   }
275
276   /* Get the IP header length */
277   ihl = tvb_get_guint8(tvb, 0) & IP_HDR_LEN_MASK;
278   ihl <<= 2;
279
280   /* Check IP header length */
281   if(ihl < IP_HDR_LEN) {
282     if(check_col(pinfo->cinfo, COL_INFO)) {
283       col_add_fstr(pinfo->cinfo, COL_INFO, "VJ uncompressed TCP (IP header length (%u) < %u)",
284                    ihl, IP_HDR_LEN);
285     }
286     if(cs != NULL)
287       cs->flags |= SLF_TOSS;
288     return;
289   }
290
291   /* Make sure we have the full IP header */
292   if(isize < ihl) {
293     if(check_col(pinfo->cinfo, COL_INFO))
294       col_set_str(pinfo->cinfo, COL_INFO, "VJ uncompressed TCP (not enough data available)");
295     if(tree != NULL)
296       call_dissector(data_handle, tvb, pinfo, tree);
297     if(cs != NULL)
298       cs->flags |= SLF_TOSS;
299     return;
300   }
301
302   if(check_col(pinfo->cinfo, COL_INFO))
303     col_set_str(pinfo->cinfo, COL_INFO, "VJ uncompressed TCP");
304
305   /*
306    * Copy packet data to a buffer, and replace the connection index with
307    * the protocol type (which is always TCP), to give the actual IP header.
308    */
309   buffer = tvb_memdup(tvb, 0, isize);
310   buffer[IP_FIELD_PROTOCOL] = IP_PROTO_TCP;
311
312   /* Check IP checksum */
313   if(ip_csum(buffer, ihl) != ZERO) {
314     /*
315      * Checksum invalid - don't update state, and don't decompress
316      * any subsequent compressed packets in this direction.
317      */
318     if(cs != NULL)
319       cs->flags |= SLF_TOSS;
320     cs = NULL;  /* disable state updates */
321   } else {
322     /* Do we have the TCP header length in the tvbuff? */
323     if(!tvb_bytes_exist(tvb, ihl + TCP_FIELD_HDR_LEN, 1)) {
324       /* We don't, so we can't provide enough data for decompression */
325       if(check_col(pinfo->cinfo, COL_INFO))
326         col_set_str(pinfo->cinfo, COL_INFO, "VJ uncompressed TCP (not enough data available)");
327       if(cs != NULL)
328         cs->flags |= SLF_TOSS;
329       cs = NULL;  /* disable state updates */
330     } else {
331       /* Get the TCP header length */
332       thl = tvb_get_guint8(tvb, ihl + TCP_FIELD_HDR_LEN);
333       thl = ((thl & 0xf0) >> 4) * 4;
334
335       /* Check TCP header length */
336       if(thl < TCP_HDR_LEN) {
337         if(check_col(pinfo->cinfo, COL_INFO)) {
338           col_add_fstr(pinfo->cinfo, COL_INFO, "VJ uncompressed TCP (TCP header length (%u) < %u)",
339                        thl, TCP_HDR_LEN);
340         }
341         if(cs != NULL)
342           cs->flags |= SLF_TOSS;
343         cs = NULL;  /* disable state updates */
344       } else {
345         /* Make sure we have the full TCP header */
346         if(isize < thl) {
347           if(check_col(pinfo->cinfo, COL_INFO))
348             col_set_str(pinfo->cinfo, COL_INFO, "VJ uncompressed TCP (not enough data available)");
349           if(cs != NULL)
350             cs->flags |= SLF_TOSS;
351           cs = NULL;  /* disable state updates */
352         }
353       }
354     }
355   }
356
357   /*
358    * If packet seen for first time, update state if we have state and can
359    * update it.
360    */
361   if(!pinfo->fd->flags.visited) {
362     if(cs != NULL) {
363       cs->flags &= ~SLF_TOSS;
364       memcpy(&cs->cs_ip, &buffer[0], IP_HDR_LEN);
365       memcpy(&cs->cs_tcp, &buffer[ihl], TCP_HDR_LEN);
366       if(ihl > IP_HDR_LEN)
367         memcpy(cs->cs_ipopt, &buffer[sizeof(iphdr_type)], ihl - IP_HDR_LEN);
368       if(TCP_OFFSET(&(cs->cs_tcp)) > 5)
369         memcpy(cs->cs_tcpopt, &buffer[ihl + sizeof(tcphdr_type)],
370                    (TCP_OFFSET(&(cs->cs_tcp)) - 5) * 4);
371     }
372   }
373
374   /*
375    * Set up tvbuff containing packet with protocol type.
376    * Neither header checksum is recalculated.
377    *
378    * Use the length field from the IP header as the reported length;
379    * use the minimum of that and the number of bytes we got from
380    * the tvbuff as the actual length, just in case the tvbuff we were
381    * handed includes part or all of the FCS (because the FCS preference
382    * for the PPP dissector doesn't match the FCS size in this session).
383    */
384   ipsize = pntohs(&buffer[IP_FIELD_TOT_LEN]);
385   if (ipsize < isize)
386     isize = ipsize;
387   next_tvb = tvb_new_child_real_data(tvb, buffer, isize, ipsize);
388   add_new_data_source(pinfo, next_tvb, "VJ Uncompressed");
389
390   /*
391    * Call IP dissector.
392    */
393   call_dissector(ip_handle, next_tvb, pinfo, tree);
394 }
395
396 /* Dissector for VJ Compressed packets */
397 static void
398 dissect_vjc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
399 {
400   proto_item *ti;
401   proto_tree *vj_tree     = NULL;
402   tvbuff_t   *next_tvb = NULL;
403   slcompress *comp     = NULL;
404   gint        err      = VJ_ERROR;
405
406   col_set_str(pinfo->cinfo, COL_PROTOCOL, "PPP VJ");
407
408   if(tree != NULL) {
409     ti = proto_tree_add_protocol_format(tree, proto_vj, tvb, 0, -1,
410                                         "PPP VJ Compression: Compressed data");
411     vj_tree = proto_item_add_subtree(ti, ett_vj);
412   }
413
414   if(!ppp_vj_decomp || pinfo->p2p_dir == P2P_DIR_UNKNOWN) {
415     /*
416      * VJ decompression turned off, so we shouldn't decompress, or
417      * direction of the traffic unknown, so we can't decompress.
418      */
419     comp = NULL;
420   } else {
421     /* Get state for that direction */
422     comp = rx_tx_state[pinfo->p2p_dir];
423   }
424
425   /* Process the compressed data header */
426   if(vjc_process(tvb, pinfo, vj_tree, comp) == VJ_ERROR)
427     return;
428
429   /* Decompression possible - set up tvb containing decompressed packet */
430   err = vjc_tvb_setup(tvb, &next_tvb, pinfo);
431   if(err == VJ_ERROR) {
432     if(tree != NULL)
433       call_dissector(data_handle, tvb, pinfo, vj_tree);
434     return;
435   }
436
437   /* No errors, so call IP dissector */
438   call_dissector(ip_handle, next_tvb, pinfo, tree);
439 }
440
441 /* Initialization function */
442 static void
443 vj_init(void)
444 {
445   gint i           = ZERO;
446
447   for(i = 0; i < RX_TX_STATE_COUNT; i++) {
448     rx_tx_state[i] = slhc_init();
449   }
450   return;
451 }
452
453 /* Initialization routine for VJ decompression */
454 static slcompress *
455 slhc_init(void)
456 {
457   slcompress *comp = se_alloc(sizeof(slcompress));
458   int         i;
459
460   memset(comp, ZERO, sizeof(slcompress));
461
462   /*
463    * Initialize the state; there is no current connection, and
464    * we have no header data for any of the connections, as we
465    * haven't yet seen an uncompressed frame.
466    */
467   comp->recv_current = TCP_SIMUL_CONV_MAX - 1;
468   for (i = 0; i < TCP_SIMUL_CONV_MAX; i++)
469     comp->rstate[i].flags |= SLF_TOSS;
470   return comp;
471 }
472
473 /* Setup the decompressed packet tvb for VJ compressed packets */
474 static gint
475 vjc_tvb_setup(tvbuff_t *src_tvb,
476               tvbuff_t **dst_tvb,
477               packet_info *pinfo)
478 {
479   vj_header_t *hdr_buf;
480   guint8       offset;
481   guint8      *data_ptr;
482   gint         hdr_len;
483   gint         tot_len;
484   gint         buf_len;
485   guint8      *pbuf;
486
487   DISSECTOR_ASSERT(src_tvb);
488
489   /* Get decompressed header stored in fd protocol area */
490   hdr_buf = p_get_proto_data(pinfo->fd, proto_vj);
491   if(hdr_buf == NULL) {
492     if(check_col(pinfo->cinfo, COL_INFO))
493       col_set_str(pinfo->cinfo, COL_INFO, "VJ compressed TCP (previous data bad or missing)");
494     return VJ_ERROR;
495   }
496
497   if(check_col(pinfo->cinfo, COL_INFO))
498     col_set_str(pinfo->cinfo, COL_INFO, "VJ compressed TCP");
499
500   /* Get the data offset in the tvbuff */
501   offset  = hdr_buf->offset;
502
503   /* Copy header and form tvb */
504   data_ptr = hdr_buf->data;
505   hdr_len  = lo_nibble(data_ptr[0]) * 4;
506   hdr_len += hi_nibble(data_ptr[hdr_len + 12]) * 4;
507   buf_len  = tvb_length(src_tvb) + hdr_len - offset;
508   pbuf     = g_malloc(buf_len);
509   memcpy(pbuf, data_ptr, hdr_len);
510   tvb_memcpy(src_tvb, pbuf + hdr_len, offset, buf_len - hdr_len);
511   memcpy(&tot_len, data_ptr + 2, 2);
512   *dst_tvb = tvb_new_child_real_data(src_tvb, pbuf, buf_len, g_ntohs(tot_len));
513   add_new_data_source(pinfo, *dst_tvb, "VJ Decompressed");
514   return VJ_OK;
515 }
516
517 /*
518  * For VJ compressed packet:
519  *
520  *      check if it is malformed;
521  *      dissect the relevant fields;
522  *      update the decompressor state on the first pass.
523  */
524 static gint
525 vjc_process(tvbuff_t *src_tvb, packet_info *pinfo, proto_tree *tree,
526             slcompress *comp)
527 {
528   int            offset     = ZERO;
529   int            i;
530   gint           changes;
531   proto_item    *ti;
532   proto_tree    *changes_tree;
533   guint8         conn_index;
534   cstate        *cs         = NULL;
535   iphdr_type    *ip         = NULL;
536   tcphdr_type   *thp        = NULL;
537   guint16        tcp_cksum;
538   gint           hdrlen     = ZERO;
539   guint16        word;
540   int            delta;
541   gint           len;
542   vj_header_t   *buf_hdr;
543   guint8        *data_ptr;
544
545   if(tvb_length(src_tvb) < 3){
546     /*
547      * We don't even have enough data for the change byte, so we can't
548      * determine which connection this is; mark all connections as
549      * non-decompressible.
550      */
551     if(check_col(pinfo->cinfo, COL_INFO))
552       col_set_str(pinfo->cinfo, COL_INFO, "VJ compressed TCP (not enough data available)");
553     if(tree != NULL)
554       call_dissector(data_handle, src_tvb, pinfo, tree);
555     if(comp != NULL) {
556       for(i = 0; i < TCP_SIMUL_CONV_MAX; i++)
557         comp->rstate[i].flags |= SLF_TOSS;
558     }
559     return VJ_ERROR;
560   }
561
562   /* Read the change byte */
563   changes = tvb_get_guint8(src_tvb, offset);
564   if(tree != NULL) {
565     switch (changes & SPECIALS_MASK) {
566
567     case SPECIAL_I:
568       ti = proto_tree_add_uint_format(tree, hf_vj_change_mask, src_tvb,
569                                       offset, 1, changes,
570                                       "Change mask: 0x%02x (echoed interactive traffic)",
571                                       changes);
572       break;
573
574     case SPECIAL_D:
575       ti = proto_tree_add_uint_format(tree, hf_vj_change_mask, src_tvb,
576                                       offset, 1, changes,
577                                       "Change mask: 0x%02x (unidirectional data)",
578                                       changes);
579       break;
580
581     default:
582       /*
583        * XXX - summarize bits?
584        */
585       ti = proto_tree_add_uint_format(tree, hf_vj_change_mask, src_tvb,
586                                       offset, 1, changes,
587                                       "Change mask: 0x%02x", changes);
588       break;
589     }
590     changes_tree = proto_item_add_subtree(ti, ett_vj_changes);
591     proto_tree_add_boolean(changes_tree, hf_vj_change_mask_c, src_tvb,
592                            offset, 1, changes);
593     proto_tree_add_boolean(changes_tree, hf_vj_change_mask_i, src_tvb,
594                            offset, 1, changes);
595     proto_tree_add_boolean(changes_tree, hf_vj_change_mask_p, src_tvb,
596                            offset, 1, changes);
597     proto_tree_add_boolean(changes_tree, hf_vj_change_mask_s, src_tvb,
598                            offset, 1, changes);
599     proto_tree_add_boolean(changes_tree, hf_vj_change_mask_a, src_tvb,
600                            offset, 1, changes);
601     proto_tree_add_boolean(changes_tree, hf_vj_change_mask_w, src_tvb,
602                            offset, 1, changes);
603     proto_tree_add_boolean(changes_tree, hf_vj_change_mask_u, src_tvb,
604                            offset, 1, changes);
605   }
606   offset++;
607
608   if(changes & NEW_C){    /* Read conn index */
609     conn_index = tvb_get_guint8(src_tvb, offset);
610     if(tree != NULL)
611       proto_tree_add_uint(tree, hf_vj_connection_number, src_tvb, offset, 1,
612                           conn_index);
613     offset++;
614     if(comp != NULL)
615       comp->recv_current = conn_index;
616   }
617
618   if(!pinfo->fd->flags.visited) {
619     /*
620      * This is the first time this frame has been seen, so we need
621      * state information to decompress it.  If that information isn't
622      * available, don't use the state information, and don't update it,
623      * either.
624      */
625     if(comp != NULL && !(comp->rstate[comp->recv_current].flags & SLF_TOSS)) {
626       cs = &comp->rstate[comp->recv_current];
627       thp = &cs->cs_tcp;
628       ip = &cs->cs_ip;
629     }
630   }
631
632   /* Build TCP and IP headers */
633   tcp_cksum = tvb_get_ntohs(src_tvb, offset);
634   if(tree != NULL)
635     proto_tree_add_uint(tree, hf_vj_tcp_cksum, src_tvb, offset, 2, tcp_cksum);
636   if(cs != NULL) {
637     hdrlen = lo_nibble(ip->ihl_version) * 4 + TCP_OFFSET(thp) * 4;
638     thp->cksum = g_htons(tcp_cksum);
639   }
640   offset += 2;
641   if(cs != NULL) {
642     if(changes & CHANGE_PUSH_BIT)
643       thp->flags |= TCP_PUSH_BIT;
644     else
645       thp->flags &= ~TCP_PUSH_BIT;
646   }
647
648   /* Deal with special cases and normal deltas */
649   switch(changes & SPECIALS_MASK){
650     case SPECIAL_I:                   /* Echoed terminal traffic */
651       if(cs != NULL) {
652         word = g_ntohs(ip->tot_len) - hdrlen;
653         thp->ack_seq = g_htonl(g_ntohl(thp->ack_seq) + word);
654         thp->seq = g_htonl(g_ntohl(thp->seq) + word);
655       }
656       break;
657     case SPECIAL_D:                   /* Unidirectional data */
658       if(cs != NULL)
659         thp->seq = g_htonl(g_ntohl(thp->seq) + g_ntohs(ip->tot_len) - hdrlen);
660       break;
661     default:
662       if(changes & NEW_U){
663         delta = get_unsigned_delta(src_tvb, &offset, hf_vj_urp, tree);
664         if(cs != NULL) {
665           thp->urg_ptr = delta;
666           thp->flags |= TCP_URG_BIT;
667         }
668       } else {
669         if(cs != NULL)
670           thp->flags &= ~TCP_URG_BIT;
671       }
672       if(changes & NEW_W) {
673         delta = get_signed_delta(src_tvb, &offset, hf_vj_win_delta, tree);
674         if(cs != NULL)
675           thp->window = g_htons(g_ntohs(thp->window) + delta);
676       }
677       if(changes & NEW_A) {
678         delta = get_unsigned_delta(src_tvb, &offset, hf_vj_ack_delta, tree);
679         if(cs != NULL)
680           thp->ack_seq = g_htonl(g_ntohl(thp->ack_seq) + delta);
681       }
682       if(changes & NEW_S) {
683         delta = get_unsigned_delta(src_tvb, &offset, hf_vj_seq_delta, tree);
684         if(cs != NULL)
685           thp->seq = g_htonl(g_ntohl(thp->seq) + delta);
686       }
687       break;
688   }
689   if(changes & NEW_I)
690     delta = get_unsigned_delta(src_tvb, &offset, hf_vj_ip_id_delta, tree);
691   else
692     delta = 1;
693   if(cs != NULL)
694     ip->id = g_htons(g_ntohs(ip->id) + delta);
695
696   /* Compute IP packet length and the buffer length needed */
697   len = tvb_reported_length_remaining(src_tvb, offset);
698   if(len < ZERO) {
699     /*
700      * This shouldn't happen, as we *were* able to fetch stuff right before
701      * offset.
702      */
703     if(check_col(pinfo->cinfo, COL_INFO))
704       col_set_str(pinfo->cinfo, COL_INFO, "VJ compressed TCP (not enough data available)");
705     if(cs != NULL)
706       cs->flags |= SLF_TOSS;
707     return VJ_ERROR;
708   }
709
710   /* Show the TCP payload */
711   if(tree != NULL && tvb_offset_exists(src_tvb, offset))
712     proto_tree_add_text(tree, src_tvb, offset, -1, "TCP payload");
713
714   /* Nothing more to do if we don't have any compression state */
715   if(comp == NULL) {
716     /* Direction of the traffic unknown - can't decompress */
717     if(check_col(pinfo->cinfo, COL_INFO))
718       col_set_str(pinfo->cinfo, COL_INFO, "VJ compressed TCP (direction unknown)");
719     return VJ_ERROR;
720   }
721
722   if((cs != NULL) && (!pinfo->fd->flags.visited)) {
723     len += hdrlen;
724     ip->tot_len = g_htons(len);
725     /* Compute IP check sum */
726     ip->cksum = ZERO;
727     ip->cksum = ip_csum((guint8 *)ip, lo_nibble(ip->ihl_version) * 4);
728
729     /* Store the reconstructed header in frame data area */
730     buf_hdr = se_alloc(sizeof (vj_header_t));
731     buf_hdr->offset = offset;  /* Offset in tvbuff is also stored */
732     data_ptr = buf_hdr->data;
733     memcpy(data_ptr, ip, IP_HDR_LEN);
734     data_ptr += IP_HDR_LEN;
735     if(lo_nibble(ip->ihl_version) > 5) {
736       memcpy(data_ptr, cs->cs_ipopt, (lo_nibble(ip->ihl_version) - 5) * 4);
737       data_ptr += (lo_nibble(ip->ihl_version) - 5) * 4;
738     }
739     memcpy(data_ptr, thp, TCP_HDR_LEN);
740     data_ptr += TCP_HDR_LEN;
741     if(TCP_OFFSET(thp) > 5)
742       memcpy(data_ptr, cs->cs_tcpopt, (TCP_OFFSET(thp) - 5) * 4);
743     p_add_proto_data(pinfo->fd, proto_vj, buf_hdr);
744   }
745
746   return VJ_OK;
747 }
748
749 /*
750  * Get an unsigned delta for a field, and put it into the protocol tree if
751  * we're building a protocol tree.
752  */
753 static int
754 get_unsigned_delta(tvbuff_t *tvb, int *offsetp, int hf, proto_tree *tree)
755 {
756   int offset = *offsetp;
757   int len;
758   guint16 del;
759
760   len = 1;
761   del = tvb_get_guint8(tvb, offset++);
762   if(del == ZERO){
763     del = tvb_get_ntohs(tvb, offset);
764     offset += 2;
765     len += 2;
766   }
767   if(tree != NULL)
768     proto_tree_add_uint(tree, hf, tvb, *offsetp, len, del);
769   *offsetp = offset;
770   return del;
771 }
772
773 /*
774  * Get a signed delta for a field, and put it into the protocol tree if
775  * we're building a protocol tree.
776  */
777 static int
778 get_signed_delta(tvbuff_t *tvb, int *offsetp, int hf, proto_tree *tree)
779 {
780   int offset = *offsetp;
781   int len;
782   gint16 del;
783
784   len = 1;
785   del = tvb_get_guint8(tvb, offset++);
786   if(del == ZERO){
787     del = tvb_get_ntohs(tvb, offset);
788     offset += 2;
789     len += 2;
790   }
791   if(tree != NULL)
792     proto_tree_add_int(tree, hf, tvb, *offsetp, len, del);
793   *offsetp = offset;
794   return del;
795 }
796
797 /* Wrapper for in_cksum function */
798 static guint16
799 ip_csum(const guint8 * ptr, guint32 len)
800 {
801   vec_t cksum_vec[1];
802
803   cksum_vec[0].ptr = ptr;
804   cksum_vec[0].len = len;
805   return in_cksum(&cksum_vec[0], 1);
806 }
807
808 /* Registration functions for dissectors */
809 void
810 proto_register_vj(void)
811 {
812   static hf_register_info hf[] = {
813     { &hf_vj_change_mask,
814       { "Change mask",  "vj.change_mask",       FT_UINT8, BASE_HEX,
815         NULL, 0x0, NULL, HFILL }},
816     { &hf_vj_change_mask_c,
817       { "Connection changed",           "vj.change_mask_c",     FT_BOOLEAN, 8,
818         NULL, NEW_C, "Connection number changed", HFILL }},
819     { &hf_vj_change_mask_i,
820       { "IP ID change != 1",            "vj.change_mask_i",     FT_BOOLEAN, 8,
821         NULL, NEW_I, "IP ID changed by a value other than 1", HFILL }},
822     { &hf_vj_change_mask_p,
823       { "Push bit set",                 "vj.change_mask_p",     FT_BOOLEAN, 8,
824         NULL, CHANGE_PUSH_BIT, "TCP PSH flag set", HFILL }},
825     { &hf_vj_change_mask_s,
826       { "Sequence number changed",      "vj.change_mask_s",     FT_BOOLEAN, 8,
827         NULL, NEW_S, NULL, HFILL }},
828     { &hf_vj_change_mask_a,
829       { "Ack number changed",           "vj.change_mask_a",     FT_BOOLEAN, 8,
830         NULL, NEW_A, "Acknowledgement sequence number changed", HFILL }},
831     { &hf_vj_change_mask_w,
832       { "Window changed",               "vj.change_mask_w",     FT_BOOLEAN, 8,
833         NULL, NEW_W, "TCP window changed", HFILL }},
834     { &hf_vj_change_mask_u,
835       { "Urgent pointer set",           "vj.change_mask_u",     FT_BOOLEAN, 8,
836         NULL, NEW_U, NULL, HFILL }},
837     { &hf_vj_connection_number,
838       { "Connection number",    "vj.connection_number", FT_UINT8, BASE_DEC,
839         NULL, 0x0, NULL, HFILL }},
840     { &hf_vj_tcp_cksum,
841       { "TCP checksum",                 "vj.tcp_cksum", FT_UINT16, BASE_HEX,
842         NULL, 0x0, NULL, HFILL }},
843     { &hf_vj_urp,
844       { "Urgent pointer",               "vj.urp",       FT_UINT16, BASE_DEC,
845         NULL, 0x0, NULL, HFILL }},
846     { &hf_vj_win_delta,
847       { "Window delta",                 "vj.win_delta", FT_INT16, BASE_DEC,
848         NULL, 0x0, "Delta for window", HFILL }},
849     { &hf_vj_ack_delta,
850       { "Ack delta",                    "vj.ack_delta", FT_UINT16, BASE_DEC,
851         NULL, 0x0, "Delta for acknowledgment sequence number", HFILL }},
852     { &hf_vj_seq_delta,
853       { "Sequence delta",               "vj.seq_delta", FT_UINT16, BASE_DEC,
854         NULL, 0x0, "Delta for sequence number", HFILL }},
855     { &hf_vj_ip_id_delta,
856       { "IP ID delta",                  "vj.ip_id_delta",       FT_UINT16, BASE_DEC,
857         NULL, 0x0, "Delta for IP ID", HFILL }},
858   };
859   static gint *ett[] = {
860     &ett_vj,
861     &ett_vj_changes,
862   };
863
864   proto_vj = proto_register_protocol("PPP VJ Compression", "PPP VJ", "vj");
865   proto_register_field_array(proto_vj, hf, array_length(hf));
866   proto_register_subtree_array(ett, array_length(ett));
867   register_init_routine(&vj_init);
868 }
869
870 void
871 proto_reg_handoff_vj(void)
872 {
873   dissector_handle_t vjc_handle;
874   dissector_handle_t vjuc_handle;
875
876   vjc_handle = create_dissector_handle(dissect_vjc, proto_vj);
877   dissector_add("ppp.protocol", PPP_VJC_COMP, vjc_handle);
878
879   vjuc_handle = create_dissector_handle(dissect_vjuc, proto_vj);
880   dissector_add("ppp.protocol", PPP_VJC_UNCOMP, vjuc_handle);
881
882   ip_handle = find_dissector("ip");
883   data_handle = find_dissector("data");
884 }
885