make KRB_SAFE more consistent with the other PDUs by removing the SAFE expansion.
[obnox/wireshark/wip.git] / packet-wtp.c
1 /* packet-wtp.c
2  *
3  * Routines to dissect WTP component of WAP traffic.
4  *
5  * $Id: packet-wtp.c,v 1.63 2004/02/28 22:56:36 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * WAP dissector based on original work by Ben Fowler
12  * Updated by Neil Hunter <neil.hunter@energis-squared.com>
13  * WTLS support by Alexandre P. Ferreira (Splice IP)
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #ifdef NEED_SNPRINTF_H
38 # include "snprintf.h"
39 #endif
40
41 #include <string.h>
42 #include <glib.h>
43 #include <epan/packet.h>
44 #include "reassemble.h"
45 #include "packet-wap.h"
46 #include "packet-wtp.h"
47 #include "packet-wsp.h"
48
49 static const true_false_string continue_truth = {
50     "TPI Present" ,
51     "No TPI"
52 };
53
54 static const true_false_string RID_truth = {
55     "Re-Transmission",
56     "First transmission"
57 };
58
59 static const true_false_string TIDNew_truth = {
60     "TID is new" ,
61     "TID is valid"
62 };
63
64 static const true_false_string tid_response_truth = {
65     "Response" ,
66     "Original"
67 };
68
69 static const true_false_string UP_truth = {
70     "User Acknowledgement required" ,
71     "User Acknowledgement optional"
72 };
73
74 static const true_false_string TVETOK_truth = {
75     "True",
76     "False"
77 };
78
79 static const value_string vals_wtp_pdu_type[] = {
80     { 0, "Not Allowed" },
81     { 1, "Invoke" },
82     { 2, "Result" },
83     { 3, "Ack" },
84     { 4, "Abort" },
85     { 5, "Segmented Invoke" },
86     { 6, "Segmented Result" },
87     { 7, "Negative Ack" },
88     { 0, NULL }
89 };
90
91 static const value_string vals_transaction_trailer[] = {
92     { 0, "Not last packet" },
93     { 1, "Last packet of message" },
94     { 2, "Last packet of group" },
95     { 3, "Re-assembly not supported" },
96     { 0, NULL }
97 };
98
99 static const value_string vals_version[] = {
100     { 0, "Current" },
101     { 1, "Undefined" },
102     { 2, "Undefined" },
103     { 3, "Undefined" },
104     { 0, NULL }
105 };
106
107 static const value_string vals_abort_type[] = {
108     { 0, "Provider" },
109     { 1, "User (WSP)" },
110     { 0, NULL }
111 };
112
113 static const value_string vals_abort_reason_provider[] = {
114     { 0x00, "Unknown" },
115     { 0x01, "Protocol Error" },
116     { 0x02, "Invalid TID" },
117     { 0x03, "Not Implemented Class 2" },
118     { 0x04, "Not Implemented SAR" },
119     { 0x05, "Not Implemented User Acknowledgement" },
120     { 0x06, "WTP Version Zero" },
121     { 0x07, "Capacity Temporarily Exceeded" },
122     { 0x08, "No Response" },
123     { 0x09, "Message Too Large" },
124     { 0x00, NULL }
125 };
126
127 static const value_string vals_transaction_classes[] = {
128     { 0x00, "Unreliable Invoke without Result" },
129     { 0x01, "Reliable Invoke without Result" },
130     { 0x02, "Reliable Invoke with Reliable Result" },
131     { 0x00, NULL }
132 };
133
134 static const value_string vals_tpi_type[] = {
135     { 0x00, "Error" },
136     { 0x01, "Info" },
137     { 0x02, "Option" },
138     { 0x03, "Packet sequence number" },
139     { 0x04, "SDU boundary" },
140     { 0x05, "Frame boundary" },
141     { 0x00, NULL }
142 };
143
144 static const value_string vals_tpi_opt[] = {
145     { 0x01, "Maximum receive unit" },
146     { 0x02, "Total message size" },
147     { 0x03, "Delay transmission timer" },
148     { 0x04, "Maximum group" },
149     { 0x05, "Current TID" },
150     { 0x06, "No cached TID" },
151     { 0x00, NULL }
152 };
153
154 /* File scoped variables for the protocol and registered fields */
155 static int proto_wtp                            = HF_EMPTY;
156
157 /* These fields used by fixed part of header */
158 static int hf_wtp_header_sub_pdu_size           = HF_EMPTY;
159 static int hf_wtp_header_flag_continue          = HF_EMPTY;
160 static int hf_wtp_header_pdu_type               = HF_EMPTY;
161 static int hf_wtp_header_flag_Trailer           = HF_EMPTY;
162 static int hf_wtp_header_flag_RID               = HF_EMPTY;
163 static int hf_wtp_header_flag_TID               = HF_EMPTY;
164 static int hf_wtp_header_flag_TID_response      = HF_EMPTY;
165
166 /* These fields used by Invoke packets */
167 static int hf_wtp_header_Inv_version            = HF_EMPTY;
168 static int hf_wtp_header_Inv_flag_TIDNew        = HF_EMPTY;
169 static int hf_wtp_header_Inv_flag_UP            = HF_EMPTY;
170 static int hf_wtp_header_Inv_Reserved           = HF_EMPTY;
171 static int hf_wtp_header_Inv_TransactionClass   = HF_EMPTY;
172
173
174 static int hf_wtp_header_variable_part          = HF_EMPTY;
175 static int hf_wtp_data                          = HF_EMPTY;
176
177 static int hf_wtp_tpi_type                      = HF_EMPTY;
178 static int hf_wtp_tpi_psn                       = HF_EMPTY;
179 static int hf_wtp_tpi_opt                       = HF_EMPTY;
180 static int hf_wtp_tpi_optval                    = HF_EMPTY;
181 static int hf_wtp_tpi_info                      = HF_EMPTY;
182
183 static int hf_wtp_header_Ack_flag_TVETOK        = HF_EMPTY;
184 static int hf_wtp_header_Abort_type             = HF_EMPTY;
185 static int hf_wtp_header_Abort_reason_provider  = HF_EMPTY;
186 static int hf_wtp_header_Abort_reason_user      = HF_EMPTY;
187 static int hf_wtp_header_sequence_number        = HF_EMPTY;
188 static int hf_wtp_header_missing_packets        = HF_EMPTY;
189
190 /* These fields used when reassembling WTP fragments */
191 static int hf_wtp_fragments                     = HF_EMPTY;
192 static int hf_wtp_fragment                      = HF_EMPTY;
193 static int hf_wtp_fragment_overlap              = HF_EMPTY;
194 static int hf_wtp_fragment_overlap_conflict     = HF_EMPTY;
195 static int hf_wtp_fragment_multiple_tails       = HF_EMPTY;
196 static int hf_wtp_fragment_too_long_fragment    = HF_EMPTY;
197 static int hf_wtp_fragment_error                = HF_EMPTY;
198 static int hf_wtp_reassembled_in                = HF_EMPTY;
199
200 /* Initialize the subtree pointers */
201 static gint ett_wtp                             = ETT_EMPTY;
202 static gint ett_wtp_sub_pdu_tree        = ETT_EMPTY;
203 static gint ett_header                          = ETT_EMPTY;
204 static gint ett_tpilist                         = ETT_EMPTY;
205 static gint ett_wsp_fragments                   = ETT_EMPTY;
206 static gint ett_wtp_fragment                    = ETT_EMPTY;
207
208 static const fragment_items wtp_frag_items = {
209     &ett_wtp_fragment,
210     &ett_wsp_fragments,
211     &hf_wtp_fragments,
212     &hf_wtp_fragment,
213     &hf_wtp_fragment_overlap,
214     &hf_wtp_fragment_overlap_conflict,
215     &hf_wtp_fragment_multiple_tails,
216     &hf_wtp_fragment_too_long_fragment,
217     &hf_wtp_fragment_error,
218     &hf_wtp_reassembled_in,
219     "fragments"
220 };
221
222 /* Handle for WSP dissector */
223 static dissector_handle_t wsp_handle;
224
225 /*
226  * reassembly of WSP
227  */
228 static GHashTable       *wtp_fragment_table = NULL;
229
230 static void
231 wtp_defragment_init(void)
232 {
233     fragment_table_init(&wtp_fragment_table);
234 }
235
236 /*
237  * Extract some bitfields
238  */
239 #define pdu_type(octet)                 (((octet) >> 3) & 0x0F) /* Note pdu type must not be 0x00 */
240 #define transaction_class(octet)        ((octet) & 0x03)        /* ......XX */
241 #define transmission_trailer(octet)     (((octet) >> 1) & 0x01) /* ......X. */
242
243 static char retransmission_indicator(unsigned char octet)
244 {
245     switch (pdu_type(octet)) {
246         case INVOKE:
247         case RESULT:
248         case ACK:
249         case SEGMENTED_INVOKE:
250         case SEGMENTED_RESULT:
251         case NEGATIVE_ACK:
252             return octet & 0x01;        /* .......X */
253         default:
254             return 0;
255     }
256 }
257
258 /*
259  * dissect a TPI
260  */
261 static void
262 wtp_handle_tpi(proto_tree *tree, tvbuff_t *tvb)
263 {
264     int                  offset = 0;
265     unsigned char        tByte;
266     unsigned char        tType;
267     unsigned char        tLen;
268     proto_item          *subTree = NULL;
269
270     tByte = tvb_get_guint8(tvb, offset++);
271     tType = (tByte & 0x78) >> 3;
272     if (tByte & 0x04)                           /* Long TPI     */
273         tLen = tvb_get_guint8(tvb, offset++);
274     else
275         tLen = tByte & 0x03;
276     subTree = proto_tree_add_uint(tree, hf_wtp_tpi_type,
277                                   tvb, 0, tvb_length(tvb), tType);
278     proto_item_add_subtree(subTree, ett_tpilist);
279     switch (tType) {
280         case 0x00:                      /* Error*/
281             /* \todo    */
282             break;
283         case 0x01:                      /* Info */
284             /* Beware, untested case here       */
285             proto_tree_add_item(subTree, hf_wtp_tpi_info,
286                                 tvb, offset, tLen, bo_little_endian);
287             break;
288         case 0x02:                      /* Option       */
289             proto_tree_add_item(subTree, hf_wtp_tpi_opt,
290                                 tvb, offset++, 1, bo_little_endian);
291             proto_tree_add_item(subTree, hf_wtp_tpi_optval,
292                                 tvb, offset, tLen - 1, bo_little_endian);
293             break;
294         case 0x03:                      /* PSN  */
295             proto_tree_add_item(subTree, hf_wtp_tpi_psn,
296                                 tvb, offset, 1, bo_little_endian);
297             break;
298         case 0x04:                      /* SDU boundary */
299             /* \todo    */
300             break;
301         case 0x05:                      /* Frame boundary       */
302             /* \todo    */
303             break;
304         default:
305             break;
306     }
307 }
308
309 /* Code to actually dissect the packets */
310 static void
311 dissect_wtp_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
312 {
313     static GString *szInfo = NULL;
314     int         offCur          = 0; /* current offset from start of WTP data */
315
316     unsigned char  b0;
317
318     /* continuation flag */
319     unsigned char       fCon;                   /* Continue flag        */
320     unsigned char       fRID;                   /* Re-transmission indicator*/
321     unsigned char       fTTR = '\0';            /* Transmission trailer */
322     guint               cbHeader        = 0;    /* Fixed header length  */
323     guint               vHeader         = 0;    /* Variable header length*/
324     int                 abortType       = 0;
325
326     /* Set up structures we'll need to add the protocol subtree and manage it */
327     proto_item          *ti = NULL;
328     proto_tree          *wtp_tree = NULL;
329
330     char                pdut;
331     char                clsTransaction  = ' ';
332     int                 numMissing = 0;         /* Number of missing packets in a negative ack */
333     int                 i;
334     tvbuff_t            *wsp_tvb = NULL;
335     guint8              psn = 0;                /* Packet sequence number*/
336     guint16             TID = 0;                /* Transaction-Id       */
337     int                 dataOffset;
338     gint                dataLen;
339
340     if (szInfo == NULL)
341         szInfo = g_string_sized_new(32);
342
343     b0 = tvb_get_guint8 (tvb, offCur + 0);
344     /* Discover Concatenated PDUs */
345     if (b0 == 0) {
346         guint   c_fieldlen = 0;         /* Length of length-field       */
347         guint   c_pdulen = 0;           /* Length of conc. PDU  */
348
349         if (tree) {
350             ti = proto_tree_add_item(tree, proto_wtp,
351                                     tvb, offCur, 1, bo_little_endian);
352             wtp_tree = proto_item_add_subtree(ti, ett_wtp_sub_pdu_tree);
353                 proto_item_append_text(ti, ", PDU concatenation");
354         }
355         offCur = 1;
356         i = 1;
357         while (offCur < (int) tvb_reported_length(tvb)) {
358             tvbuff_t *wtp_tvb;
359             /* The length of an embedded WTP PDU is coded as either:
360              *  - a 7-bit value contained in one octet with highest bit == 0.
361              *  - a 15-bit value contained in two octets (little endian)
362              *    if the 1st octet has its highest bit == 1.
363              * This means that this is NOT encoded as an uintvar-integer!!!
364              */
365             b0 = tvb_get_guint8(tvb, offCur + 0);
366             if (b0 & 0x80) {
367                 c_fieldlen = 2;
368                 c_pdulen = ((b0 & 0x7f) << 8) | tvb_get_guint8(tvb, offCur + 1);
369             } else {
370                 c_fieldlen = 1;
371                 c_pdulen = b0;
372             }
373             if (tree) {
374                 proto_tree_add_uint(wtp_tree, hf_wtp_header_sub_pdu_size,
375                                     tvb, offCur, c_fieldlen, c_pdulen);
376             }
377             if (i > 1 && check_col(pinfo->cinfo, COL_INFO)) {
378                 col_append_str(pinfo->cinfo, COL_INFO, ", ");
379             }
380             /* Skip the length field for the WTP sub-tvb */
381             wtp_tvb = tvb_new_subset(tvb, offCur + c_fieldlen, c_pdulen, c_pdulen);
382             dissect_wtp_common(wtp_tvb, pinfo, wtp_tree);
383             offCur += c_fieldlen + c_pdulen;
384             i++;
385         }
386         if (tree) {
387                 proto_item_append_text(ti, ", PDU count: %u", i);
388         }
389         return;
390     }
391     /* No concatenation */
392     fCon = b0 & 0x80;
393     fRID = retransmission_indicator(b0);
394     pdut = pdu_type(b0);
395
396 #ifdef DEBUG
397         printf("WTP packet %u: tree = %p, pdu = %s (%u) length: %u\n",
398                         pinfo->fd->num, tree,
399                         match_strval(pdut, vals_wtp_pdu_type), pdut, tvb_length(tvb));
400 #endif
401
402     /* Develop the string to put in the Info column */
403     g_string_sprintf(szInfo, "WTP %s",
404                     val_to_str(pdut, vals_wtp_pdu_type, "Unknown PDU type 0x%x"));
405
406     switch (pdut) {
407         case INVOKE:
408             fTTR = transmission_trailer(b0);
409             TID = tvb_get_ntohs(tvb, offCur + 1);
410             psn = 0;
411             clsTransaction = transaction_class(tvb_get_guint8(tvb, offCur + 3));
412             g_string_sprintfa(szInfo, " Class %d", clsTransaction);
413             cbHeader = 4;
414             break;
415
416         case SEGMENTED_INVOKE:
417         case SEGMENTED_RESULT:
418             fTTR = transmission_trailer(b0);
419             TID = tvb_get_ntohs(tvb, offCur + 1);
420             psn = tvb_get_guint8(tvb, offCur + 3);
421             if (psn != 0)
422                 g_string_sprintfa(szInfo, " (%u)", psn);
423             cbHeader = 4;
424             break;
425
426         case ABORT:
427             cbHeader = 4;
428             break;
429
430         case RESULT:
431             fTTR = transmission_trailer(b0);
432             TID = tvb_get_ntohs(tvb, offCur + 1);
433             psn = 0;
434             cbHeader = 3;
435             break;
436
437         case ACK:
438             cbHeader = 3;
439             break;
440
441         case NEGATIVE_ACK:
442             /* Variable number of missing packets */
443             numMissing = tvb_get_guint8(tvb, offCur + 3);
444             cbHeader = numMissing + 4;
445             break;
446
447         default:
448             break;
449     };
450     if (fRID) {
451         g_string_append( szInfo, " R" );
452     };
453     /* In the interest of speed, if "tree" is NULL, don't do any work not
454        necessary to generate protocol tree items. */
455     if (tree) {
456 #ifdef DEBUG
457         fprintf(stderr, "dissect_wtp: cbHeader = %d\n", cbHeader);
458 #endif
459         /* NOTE - Length will be set when we process the TPI */
460         ti = proto_tree_add_item(tree, proto_wtp, tvb, offCur, 0, bo_little_endian);
461 #ifdef DEBUG
462         fprintf(stderr, "dissect_wtp: (7) Returned from proto_tree_add_item\n");
463 #endif
464         wtp_tree = proto_item_add_subtree(ti, ett_wtp);
465
466 /* Code to process the packet goes here */
467 #ifdef DEBUG
468         fprintf(stderr, "dissect_wtp: cbHeader = %d\n", cbHeader);
469         fprintf(stderr, "dissect_wtp: offCur = %d\n", offCur);
470 #endif
471         /* Add common items: only CON and PDU Type */
472         proto_tree_add_item(
473                         wtp_tree,                       /* tree */
474                         hf_wtp_header_flag_continue,    /* id */
475                         tvb,
476                         offCur,                         /* start of highlight */
477                         1,                              /* length of highlight*/
478                         b0                              /* value */
479              );
480         proto_tree_add_item(wtp_tree, hf_wtp_header_pdu_type, tvb, offCur, 1, bo_little_endian);
481
482         switch(pdut) {
483             case INVOKE:
484                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_Trailer, tvb, offCur, 1, bo_little_endian);
485                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
486                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
487                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
488
489                 proto_tree_add_item(wtp_tree, hf_wtp_header_Inv_version , tvb, offCur + 3, 1, bo_little_endian);
490                 proto_tree_add_item(wtp_tree, hf_wtp_header_Inv_flag_TIDNew, tvb, offCur + 3, 1, bo_little_endian);
491                 proto_tree_add_item(wtp_tree, hf_wtp_header_Inv_flag_UP, tvb, offCur + 3, 1, bo_little_endian);
492                 proto_tree_add_item(wtp_tree, hf_wtp_header_Inv_Reserved, tvb, offCur + 3, 1, bo_little_endian);
493                 proto_tree_add_item(wtp_tree, hf_wtp_header_Inv_TransactionClass, tvb, offCur + 3, 1, bo_little_endian);
494                 proto_item_append_text(ti,
495                                 ", PDU: Invoke (%u)"
496                                 ", Transaction Class: %s (%u)",
497                                 INVOKE,
498                                 match_strval(clsTransaction, vals_transaction_classes),
499                                 clsTransaction);
500                 break;
501
502             case RESULT:
503                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_Trailer, tvb, offCur, 1, bo_little_endian);
504                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
505                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
506                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
507                 proto_item_append_text(ti, ", PDU: Result (%u)", RESULT);
508                 break;
509
510             case ACK:
511                 proto_tree_add_item(wtp_tree, hf_wtp_header_Ack_flag_TVETOK, tvb, offCur, 1, bo_big_endian);
512
513                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
514                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
515                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
516                 proto_item_append_text(ti, ", PDU: ACK (%u)", ACK);
517                 break;
518
519             case ABORT:
520                 abortType = tvb_get_guint8 (tvb, offCur) & 0x07;
521                 proto_tree_add_item(wtp_tree, hf_wtp_header_Abort_type , tvb, offCur , 1, bo_little_endian);
522                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
523                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
524
525                 if (abortType == PROVIDER)
526                 {
527                         guint8 reason = tvb_get_guint8(tvb, offCur + 3);
528                     proto_tree_add_item( wtp_tree, hf_wtp_header_Abort_reason_provider , tvb, offCur + 3 , 1, bo_little_endian);
529                         proto_item_append_text(ti,
530                                         ", PDU: Abort (%u)"
531                                         ", Type: Provider (%u)"
532                                         ", Reason: %s (%u)",
533                                         ABORT,
534                                         PROVIDER,
535                                         match_strval(reason, vals_abort_reason_provider), 
536                                         reason);
537                 }
538                 else if (abortType == USER)
539                 {
540                         guint8 reason = tvb_get_guint8(tvb, offCur + 3);
541                     proto_tree_add_item(wtp_tree, hf_wtp_header_Abort_reason_user , tvb, offCur + 3 , 1, bo_little_endian);
542                         proto_item_append_text(ti,
543                                         ", PDU: Abort (%u)"
544                                         ", Type: User (%u)"
545                                         ", Reason: %s (%u)",
546                                         ABORT,
547                                         PROVIDER,
548                                         match_strval(reason, vals_wsp_reason_codes), 
549                                         reason);
550                 }
551                 break;
552
553             case SEGMENTED_INVOKE:
554                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_Trailer, tvb, offCur, 1, bo_little_endian);
555                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
556                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
557                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
558
559                 proto_tree_add_item(wtp_tree, hf_wtp_header_sequence_number , tvb, offCur + 3, 1, bo_little_endian);
560                 proto_item_append_text(ti,
561                                 ", PDU: Segmented Invoke (%u)"
562                                 ", Packet Sequence Number: %u",
563                                 SEGMENTED_INVOKE, psn);
564                 break;
565
566             case SEGMENTED_RESULT:
567                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_Trailer, tvb, offCur, 1, bo_little_endian);
568                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
569                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
570                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
571
572                 proto_tree_add_item(wtp_tree, hf_wtp_header_sequence_number , tvb, offCur + 3, 1, bo_little_endian);
573                 proto_item_append_text(ti,
574                                 ", PDU: Segmented Result (%u)"
575                                 ", Packet Sequence Number: %u",
576                                 SEGMENTED_RESULT, psn);
577                 break;
578
579             case NEGATIVE_ACK:
580                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
581                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
582                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
583
584                 proto_tree_add_item(wtp_tree, hf_wtp_header_missing_packets , tvb, offCur + 3, 1, bo_little_endian);
585                 /* Iterate through missing packets */
586                 for (i = 0; i < numMissing; i++)
587                 {
588                     proto_tree_add_item(wtp_tree, hf_wtp_header_sequence_number, tvb, offCur + 4 + i, 1, bo_little_endian);
589                 }
590                 proto_item_append_text(ti,
591                                 ", PDU: Negative Ack (%u)"
592                                 ", Missing Packets: %u",
593                                 NEGATIVE_ACK, numMissing);
594                 break;
595
596             default:
597                 break;
598         };
599         if (fRID) {
600                 proto_item_append_text(ti, ", Retransmission");
601         }
602     } else { /* tree is NULL */
603 #ifdef DEBUG
604         fprintf(stderr, "dissect_wtp: (4) tree was %p\n", tree);
605 #endif
606     }
607         /* Process the variable part */
608         if (fCon) {                     /* Now, analyze variable part   */
609             unsigned char        tCon;
610             unsigned char        tByte;
611             unsigned char        tpiLen;
612             tvbuff_t            *tmp_tvb;
613
614             vHeader = 0;                /* Start scan all over  */
615
616             do {
617                 tByte = tvb_get_guint8(tvb, offCur + cbHeader + vHeader);
618                 tCon = tByte & 0x80;
619                 if (tByte & 0x04)       /* Long TPI     */
620                     tpiLen = 2 + tvb_get_guint8(tvb,
621                                             offCur + cbHeader + vHeader + 1);
622                 else
623                     tpiLen = 1 + (tByte & 0x03);
624                 if (tree)
625                 {
626                 tmp_tvb = tvb_new_subset(tvb, offCur + cbHeader + vHeader,
627                                         tpiLen, tpiLen);
628                 wtp_handle_tpi(wtp_tree, tmp_tvb);
629                 }
630                 vHeader += tpiLen;
631             } while (tCon);
632         } else {
633                 /* There is no variable part */
634         }       /* End of variable part of header */
635
636         /* Set the length of the WTP protocol part now we know the length of the
637          * fixed and variable WTP headers */
638         if (tree)
639         proto_item_set_len(ti, cbHeader + vHeader);
640
641 #ifdef DEBUG
642     fprintf( stderr, "dissect_wtp: cbHeader = %d\n", cbHeader );
643 #endif
644
645     /*
646      * Any remaining data ought to be WSP data (if not WTP ACK, NACK
647      * or ABORT pdu), so, if we have any remaining data, and it's
648      * not an ACK, NACK, or ABORT PDU, hand it off (defragmented) to the
649      * WSP dissector.
650      * Note that the last packet of a fragmented WTP message needn't
651      * contain any data, so we allow payloadless packets to be
652      * reassembled.  (XXX - does the reassembly code handle this
653      * for packets other than the last packet?)
654      *
655          * Try calling a subdissector only if:
656          *      - The WTP payload is ressembled in this very packet,
657          *      - The WTP payload is not fragmented across packets.
658          */
659     dataOffset = offCur + cbHeader + vHeader;
660     dataLen = tvb_reported_length_remaining(tvb, dataOffset);
661     if ((dataLen >= 0) &&
662                         ! ((pdut==ACK) || (pdut==NEGATIVE_ACK) || (pdut==ABORT)))
663     {
664                 /* Try to reassemble if needed, and hand over to WSP
665                  * A fragmented WTP packet is either:
666                  *      - An INVOKE with fTTR (transmission trailer) not set,
667                  *      - a SEGMENTED_INVOKE,
668                  *      - A RESULT with fTTR (transmission trailer) not set,
669                  *      - a SEGMENTED_RESULT.
670                  */
671                 if ( ( (pdut == SEGMENTED_INVOKE) || (pdut == SEGMENTED_RESULT)
672                                 || ( ((pdut == INVOKE) || (pdut == RESULT)) && (!fTTR) )
673                         ) && tvb_bytes_exist(tvb, dataOffset, dataLen) )
674                 {
675                         /* Try reassembling fragments */
676                         fragment_data *fd_wtp = NULL;
677                         guint32 reassembled_in = 0;
678                         gboolean save_fragmented = pinfo->fragmented;
679
680                         pinfo->fragmented = TRUE;
681                         fd_wtp = fragment_add_seq(tvb, dataOffset, pinfo, TID,
682                                         wtp_fragment_table, psn, dataLen, !fTTR);
683                         /* XXX - fragment_add_seq() yields NULL unless Ethereal knows
684                          * that the packet is part of a reassembled whole. This means
685                          * that fd_wtp will be NULL as long as Ethereal did not encounter
686                          * (and process) the packet containing the last fragment.
687                          * This implies that Ethereal needs two passes over the data for
688                          * correct reassembly. At the first pass, a capture containing
689                          * three fragments plus a retransmssion of the last fragment
690                          * will progressively show:
691                          *
692                          *              Packet 1: (Unreassembled fragment 1)
693                          *              Packet 2: (Unreassembled fragment 2)
694                          *              Packet 3: (Reassembled WTP)
695                          *              Packet 4: (WTP payload reassembled in packet 3)
696                          *
697                          * However at subsequent evaluation (e.g., by applying a display
698                          * filter) the packet summary will show:
699                          *
700                          *              Packet 1: (WTP payload reassembled in packet 3)
701                          *              Packet 2: (WTP payload reassembled in packet 3)
702                          *              Packet 3: (Reassembled WTP)
703                          *              Packet 4: (WTP payload reassembled in packet 3)
704                          *
705                          * This is important to know, and also affects read filters!
706                          */
707                         wsp_tvb = process_reassembled_data(tvb, dataOffset, pinfo,
708                                         "Reassembled WTP", fd_wtp, &wtp_frag_items,
709                                         NULL, wtp_tree);
710 #ifdef DEBUG
711                         printf("WTP: Packet %u %s -> %d: wsp_tvb = %p, fd_wtp = %p, frame = %u\n",
712                                         pinfo->fd->num,
713                                         fd_wtp ? "Reassembled" : "Not reassembled",
714                                         fd_wtp ? fd_wtp->reassembled_in : -1,
715                                         wsp_tvb,
716                                         fd_wtp
717                                         );
718 #endif
719                         if (fd_wtp) {
720                                 /* Reassembled */
721                                 reassembled_in = fd_wtp->reassembled_in;
722                                 if (pinfo->fd->num == reassembled_in) {
723                                         /* Reassembled in this very packet:
724                                          * We can safely hand the tvb to the WSP dissector */
725                                         call_dissector(wsp_handle, wsp_tvb, pinfo, tree);
726                                 } else {
727                                         /* Not reassembled in this packet */
728                                         if (check_col(pinfo->cinfo, COL_INFO)) {
729                                                 col_append_fstr(pinfo->cinfo, COL_INFO,
730                                                                 "%s (WTP payload reassembled in packet %u)",
731                                                                 szInfo->str, fd_wtp->reassembled_in);
732                                         }
733                                         if (tree) {
734                                                 proto_tree_add_text(wtp_tree, tvb, dataOffset, -1,
735                                                                 "Payload");
736                                         }
737                                 }
738                         } else {
739                                 /* Not reassembled yet, or not reassembled at all */
740                                 if (check_col(pinfo->cinfo, COL_INFO)) {
741                                         col_append_fstr(pinfo->cinfo, COL_INFO,
742                                                                 "%s (Unreassembled fragment %u)",
743                                                                 szInfo->str, psn);
744                                 }
745                                 if (tree) {
746                                         proto_tree_add_text(wtp_tree, tvb, dataOffset, -1,
747                                                         "Payload");
748                                 }
749                         }
750                         /* Now reset fragmentation information in pinfo */
751                         pinfo->fragmented = save_fragmented;
752                 }
753                 else if ( ((pdut == INVOKE) || (pdut == RESULT)) && (fTTR) )
754                 {
755                         /* Non-fragmented payload */
756                         wsp_tvb = tvb_new_subset(tvb, dataOffset, -1, -1);
757                         /* We can safely hand the tvb to the WSP dissector */
758                         call_dissector(wsp_handle, wsp_tvb, pinfo, tree);
759                 }
760                 else
761                 {
762                         /* Nothing to hand to subdissector */
763                         if (check_col(pinfo->cinfo, COL_INFO))
764                                 col_append_str(pinfo->cinfo, COL_INFO, szInfo->str);
765                 }
766         }
767         else
768         {
769                 /* Nothing to hand to subdissector */
770                 if (check_col(pinfo->cinfo, COL_INFO))
771                         col_append_str(pinfo->cinfo, COL_INFO, szInfo->str);
772         }
773 }
774
775 /*
776  * Called directly from UDP.
777  * Put "WTP+WSP" into the "Protocol" column.
778  */
779 static void
780 dissect_wtp_fromudp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
781 {
782     if (check_col(pinfo->cinfo, COL_PROTOCOL))
783         col_set_str(pinfo->cinfo, COL_PROTOCOL, "WTP+WSP" );
784     if (check_col(pinfo->cinfo, COL_INFO))
785         col_clear(pinfo->cinfo, COL_INFO);
786
787     dissect_wtp_common(tvb, pinfo, tree);
788 }
789
790 /*
791  * Called from a higher-level WAP dissector, presumably WTLS.
792  * Put "WTLS+WSP+WTP" to the "Protocol" column.
793  *
794  * XXX - is this supposed to be called from WTLS?  If so, we're not
795  * calling it....
796  *
797  * XXX - can this be called from any other dissector?
798  */
799 static void
800 dissect_wtp_fromwtls(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
801 {
802     if (check_col(pinfo->cinfo, COL_PROTOCOL))
803         col_set_str(pinfo->cinfo, COL_PROTOCOL, "WTLS+WTP+WSP" );
804     if (check_col(pinfo->cinfo, COL_INFO))
805         col_clear(pinfo->cinfo, COL_INFO);
806
807     dissect_wtp_common(tvb, pinfo, tree);
808 }
809
810 /* Register the protocol with Ethereal */
811 void
812 proto_register_wtp(void)
813 {
814
815     /* Setup list of header fields */
816     static hf_register_info hf[] = {
817         { &hf_wtp_header_sub_pdu_size,
818             {   "Sub PDU size",
819                 "wtp.sub_pdu_size",
820                 FT_UINT16, BASE_DEC, NULL, 0x0,
821                 "Size of Sub-PDU (bytes)", HFILL
822             }
823         },
824         { &hf_wtp_header_flag_continue,
825             {   "Continue Flag",
826                 "wtp.continue_flag",
827                 FT_BOOLEAN, 8, TFS( &continue_truth ), 0x80,
828                 "Continue Flag", HFILL
829             }
830         },
831         { &hf_wtp_header_pdu_type,
832             {   "PDU Type",
833                 "wtp.pdu_type",
834                 FT_UINT8, BASE_HEX, VALS( vals_wtp_pdu_type ), 0x78,
835                 "PDU Type", HFILL
836             }
837         },
838         { &hf_wtp_header_flag_Trailer,
839             {   "Trailer Flags",
840                 "wtp.trailer_flags",
841                 FT_UINT8, BASE_HEX, VALS( vals_transaction_trailer ), 0x06,
842                 "Trailer Flags", HFILL
843             }
844         },
845         { &hf_wtp_header_flag_RID,
846             {   "Re-transmission Indicator",
847                 "wtp.RID",
848                 FT_BOOLEAN, 8, TFS( &RID_truth ), 0x01,
849                 "Re-transmission Indicator", HFILL
850             }
851         },
852         { &hf_wtp_header_flag_TID_response,
853             {   "TID Response",
854                 "wtp.TID.response",
855                 FT_BOOLEAN, 16, TFS( &tid_response_truth ), 0x8000,
856                 "TID Response", HFILL
857             }
858         },
859         { &hf_wtp_header_flag_TID,
860             {   "Transaction ID",
861                 "wtp.TID",
862                 FT_UINT16, BASE_HEX, NULL, 0x7FFF,
863                 "Transaction ID", HFILL
864             }
865         },
866         { &hf_wtp_header_Inv_version,
867             {   "Version",
868                 "wtp.header.version",
869                 FT_UINT8, BASE_HEX, VALS( vals_version ), 0xC0,
870                 "Version", HFILL
871             }
872         },
873         { &hf_wtp_header_Inv_flag_TIDNew,
874             {   "TIDNew",
875                 "wtp.header.TIDNew",
876                 FT_BOOLEAN, 8, TFS( &TIDNew_truth ), 0x20,
877                 "TIDNew", HFILL
878             }
879         },
880         { &hf_wtp_header_Inv_flag_UP,
881             {   "U/P flag",
882                 "wtp.header.UP",
883                 FT_BOOLEAN, 8, TFS( &UP_truth ), 0x10,
884                 "U/P Flag", HFILL
885             }
886         },
887         { &hf_wtp_header_Inv_Reserved,
888             {   "Reserved",
889                 "wtp.inv.reserved",
890                 FT_UINT8, BASE_HEX, NULL, 0x0C,
891                 "Reserved", HFILL
892             }
893         },
894         { &hf_wtp_header_Inv_TransactionClass,
895             {   "Transaction Class",
896                 "wtp.inv.transaction_class",
897                 FT_UINT8, BASE_HEX, VALS( vals_transaction_classes ), 0x03,
898                 "Transaction Class", HFILL
899             }
900         },
901         { &hf_wtp_header_Ack_flag_TVETOK,
902             {   "Tve/Tok flag",
903                 "wtp.ack.tvetok",
904                 FT_BOOLEAN, 8, TFS( &TVETOK_truth ), 0x04,
905                 "Tve/Tok flag", HFILL
906             }
907         },
908         { &hf_wtp_header_Abort_type,
909             {   "Abort Type",
910                 "wtp.abort.type",
911                 FT_UINT8, BASE_HEX, VALS ( vals_abort_type ), 0x07,
912                 "Abort Type", HFILL
913             }
914         },
915         { &hf_wtp_header_Abort_reason_provider,
916             {   "Abort Reason",
917                 "wtp.abort.reason.provider",
918                 FT_UINT8, BASE_HEX, VALS ( vals_abort_reason_provider ), 0x00,
919                 "Abort Reason", HFILL
920             }
921         },
922         /* Assume WSP is the user and use its reason codes */
923         { &hf_wtp_header_Abort_reason_user,
924             {   "Abort Reason",
925                 "wtp.abort.reason.user",
926                 FT_UINT8, BASE_HEX, VALS ( vals_wsp_reason_codes ), 0x00,
927                 "Abort Reason", HFILL
928             }
929         },
930         { &hf_wtp_header_sequence_number,
931             {   "Packet Sequence Number",
932                 "wtp.header.sequence",
933                 FT_UINT8, BASE_DEC, NULL, 0x00,
934                 "Packet Sequence Number", HFILL
935             }
936         },
937         { &hf_wtp_header_missing_packets,
938             {   "Missing Packets",
939                 "wtp.header.missing_packets",
940                 FT_UINT8, BASE_DEC, NULL, 0x00,
941                 "Missing Packets", HFILL
942             }
943         },
944         { &hf_wtp_header_variable_part,
945             {   "Header: Variable part",
946                 "wtp.header_variable_part",
947                 FT_BYTES, BASE_HEX, NULL, 0x0,
948                 "Variable part of the header", HFILL
949             }
950         },
951         { &hf_wtp_data,
952             {   "Data",
953                 "wtp.header_data",
954                 FT_BYTES, BASE_HEX, NULL, 0x0,
955                 "Data", HFILL
956             }
957         },
958         { &hf_wtp_tpi_type,
959             {   "TPI",
960                 "wtp.tpi",
961                 FT_UINT8, BASE_HEX, VALS(vals_tpi_type), 0x00,
962                 "Identification of the Transport Information Item", HFILL
963             }
964         },
965         { &hf_wtp_tpi_psn,
966             {   "Packet sequence number",
967                 "wtp.tpi.psn",
968                 FT_UINT8, BASE_DEC, NULL, 0x00,
969                 "Sequence number of this packet", HFILL
970             }
971         },
972         { &hf_wtp_tpi_opt,
973             {   "Option",
974                 "wtp.tpi.opt",
975                 FT_UINT8, BASE_HEX, VALS(vals_tpi_opt), 0x00,
976                 "The given option for this TPI", HFILL
977             }
978         },
979         { &hf_wtp_tpi_optval,
980             {   "Option Value",
981                 "wtp.tpi.opt.val",
982                 FT_NONE, BASE_NONE, NULL, 0x00,
983                 "The value that is supplied with this option", HFILL
984             }
985         },
986         { &hf_wtp_tpi_info,
987             {   "Information",
988                 "wtp.tpi.info",
989                 FT_NONE, BASE_NONE, NULL, 0x00,
990                 "The information being send by this TPI", HFILL
991             }
992         },
993
994         /* Fragment fields */
995         { &hf_wtp_fragment_overlap,
996             {   "Fragment overlap",
997                 "wtp.fragment.overlap",
998                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
999                 "Fragment overlaps with other fragments", HFILL
1000             }
1001         },
1002         { &hf_wtp_fragment_overlap_conflict,
1003             {   "Conflicting data in fragment overlap",
1004                 "wtp.fragment.overlap.conflict",
1005                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1006                 "Overlapping fragments contained conflicting data", HFILL
1007             }
1008         },
1009         { &hf_wtp_fragment_multiple_tails,
1010             {   "Multiple tail fragments found",
1011                 "wtp.fragment.multipletails",
1012                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1013                 "Several tails were found when defragmenting the packet", HFILL
1014             }
1015         },
1016         { &hf_wtp_fragment_too_long_fragment,
1017             {   "Fragment too long",
1018                 "wtp.fragment.toolongfragment",
1019                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1020                 "Fragment contained data past end of packet", HFILL
1021             }
1022         },
1023         { &hf_wtp_fragment_error,
1024             {   "Defragmentation error",
1025                 "wtp.fragment.error",
1026                 FT_FRAMENUM, BASE_NONE, NULL, 0x0,
1027                 "Defragmentation error due to illegal fragments", HFILL
1028             }
1029         },
1030         { &hf_wtp_reassembled_in,
1031             {   "Reassembled in",
1032                 "wtp.reassembled.in",
1033                 FT_FRAMENUM, BASE_NONE, NULL, 0x0,
1034                 "WTP fragments are reassembled in the given packet", HFILL
1035             }
1036         },
1037         { &hf_wtp_fragment,
1038             {   "WTP Fragment",
1039                 "wtp.fragment",
1040                 FT_FRAMENUM, BASE_NONE, NULL, 0x0,
1041                 "WTP Fragment", HFILL
1042             }
1043         },
1044         { &hf_wtp_fragments,
1045             {   "WTP Fragments",
1046                 "wtp.fragments",
1047                 FT_NONE, BASE_NONE, NULL, 0x0,
1048                 "WTP Fragments", HFILL
1049             }
1050         },
1051     };
1052
1053     /* Setup protocol subtree array */
1054     static gint *ett[] = {
1055         &ett_wtp,
1056         &ett_wtp_sub_pdu_tree,
1057         &ett_header,
1058         &ett_tpilist,
1059         &ett_wsp_fragments,
1060         &ett_wtp_fragment,
1061     };
1062
1063     /* Register the protocol name and description */
1064     proto_wtp = proto_register_protocol(
1065         "Wireless Transaction Protocol",   /* protocol name for use by ethereal */
1066         "WTP",                             /* short version of name */
1067         "wtp"                      /* Abbreviated protocol name, should Match IANA
1068                                             < URL:http://www.iana.org/assignments/port-numbers/ >
1069                                             */
1070     );
1071
1072     /* Required calls to register the header fields and subtrees used */
1073     proto_register_field_array(proto_wtp, hf, array_length(hf));
1074     proto_register_subtree_array(ett, array_length(ett));
1075
1076     register_dissector("wtp-wtls", dissect_wtp_fromwtls, proto_wtp);
1077     register_dissector("wtp-udp", dissect_wtp_fromudp, proto_wtp);
1078     register_init_routine(wtp_defragment_init);
1079 }
1080
1081 void
1082 proto_reg_handoff_wtp(void)
1083 {
1084     dissector_handle_t wtp_fromudp_handle;
1085
1086     /*
1087      * Get a handle for the connection-oriented WSP dissector - if WTP
1088      * PDUs have data, it is WSP.
1089      */
1090     wsp_handle = find_dissector("wsp-co");
1091
1092     wtp_fromudp_handle = find_dissector("wtp-udp");
1093     dissector_add("udp.port", UDP_PORT_WTP_WSP, wtp_fromudp_handle);
1094     dissector_add("gsm-sms-ud.udh.port", UDP_PORT_WTP_WSP, wtp_fromudp_handle);
1095 }