Move the GSM SMS dissection to a dedicated subdissector (currently still within
[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.58 2003/12/23 12:07:14 obiot 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     fragment_data       *fd_head = NULL;
336     guint8              psn = 0;                /* Packet sequence number*/
337     guint16             TID = 0;                /* Transaction-Id       */
338
339     if (szInfo == NULL)
340         szInfo = g_string_sized_new(32);
341
342     b0 = tvb_get_guint8 (tvb, offCur + 0);
343     /* Discover Concatenated PDUs */
344     if (b0 == 0) {
345         guint   c_fieldlen = 0;         /* Length of length-field       */
346         guint   c_pdulen = 0;           /* Length of conc. PDU  */
347
348         if (tree) {
349             ti = proto_tree_add_item(tree, proto_wtp,
350                                     tvb, offCur, 1, bo_little_endian);
351             wtp_tree = proto_item_add_subtree(ti, ett_wtp_sub_pdu_tree);
352         }
353         offCur = 1;
354         i = 1;
355         while (offCur < (int) tvb_reported_length(tvb)) {
356             b0 = tvb_get_guint8(tvb, offCur + 0);
357             if (b0 & 0x80) {
358                 c_fieldlen = 2;
359                 c_pdulen = ((b0 & 0x7f) << 8) | tvb_get_guint8(tvb, offCur + 1);
360             } else {
361                 c_fieldlen = 1;
362                 c_pdulen = b0;
363             }
364             if (tree) {
365                 proto_tree_add_uint(wtp_tree, hf_wtp_header_sub_pdu_size,
366                                     tvb, offCur, c_fieldlen, c_pdulen);
367             }
368             if (i > 1 && check_col(pinfo->cinfo, COL_INFO)) {
369                 col_append_str(pinfo->cinfo, COL_INFO, ", ");
370             }
371             wsp_tvb = tvb_new_subset(tvb, offCur + c_fieldlen, c_pdulen, c_pdulen);
372             dissect_wtp_common(wsp_tvb, pinfo, wtp_tree);
373             offCur += c_fieldlen + c_pdulen;
374             i++;
375         }
376         return;
377     }
378     fCon = b0 & 0x80;
379     fRID = retransmission_indicator(b0);
380     pdut = pdu_type(b0);
381
382     /* Develop the string to put in the Info column */
383     g_string_sprintf(szInfo, "WTP %s",
384                     val_to_str(pdut, vals_wtp_pdu_type, "Unknown PDU type 0x%x"));
385
386     switch (pdut) {
387         case INVOKE:
388             fTTR = transmission_trailer(b0);
389             TID = tvb_get_ntohs(tvb, offCur + 1);
390             psn = 0;
391             clsTransaction = transaction_class(tvb_get_guint8(tvb, offCur + 3));
392             g_string_sprintfa(szInfo, " Class %d", clsTransaction);
393             cbHeader = 4;
394             break;
395
396         case SEGMENTED_INVOKE:
397         case SEGMENTED_RESULT:
398             fTTR = transmission_trailer(b0);
399             TID = tvb_get_ntohs(tvb, offCur + 1);
400             psn = tvb_get_guint8(tvb, offCur + 3);
401             cbHeader = 4;
402             break;
403
404         case ABORT:
405             cbHeader = 4;
406             break;
407
408         case RESULT:
409             fTTR = transmission_trailer(b0);
410             TID = tvb_get_ntohs(tvb, offCur + 1);
411             psn = 0;
412             cbHeader = 3;
413             break;
414
415         case ACK:
416             cbHeader = 3;
417             break;
418
419         case NEGATIVE_ACK:
420             /* Variable number of missing packets */
421             numMissing = tvb_get_guint8(tvb, offCur + 3);
422             cbHeader = numMissing + 4;
423             break;
424
425         default:
426             break;
427     };
428     if (fRID) {
429         g_string_append( szInfo, " R" );
430     };
431     /* In the interest of speed, if "tree" is NULL, don't do any work not
432        necessary to generate protocol tree items. */
433     if (tree) {
434 #ifdef DEBUG
435         fprintf(stderr, "dissect_wtp: cbHeader = %d\n", cbHeader);
436 #endif
437         /* NOTE - Length will be set when we process the TPI */
438         ti = proto_tree_add_item(tree, proto_wtp, tvb, offCur, 0, bo_little_endian);
439 #ifdef DEBUG
440         fprintf(stderr, "dissect_wtp: (7) Returned from proto_tree_add_item\n");
441 #endif
442         wtp_tree = proto_item_add_subtree(ti, ett_wtp);
443
444 /* Code to process the packet goes here */
445 #ifdef DEBUG
446         fprintf(stderr, "dissect_wtp: cbHeader = %d\n", cbHeader);
447         fprintf(stderr, "dissect_wtp: offCur = %d\n", offCur);
448 #endif
449         /* Add common items: only CON and PDU Type */
450         proto_tree_add_item(
451                         wtp_tree,                       /* tree */
452                         hf_wtp_header_flag_continue,    /* id */
453                         tvb,
454                         offCur,                         /* start of highlight */
455                         1,                              /* length of highlight*/
456                         b0                              /* value */
457              );
458         proto_tree_add_item(wtp_tree, hf_wtp_header_pdu_type, tvb, offCur, 1, bo_little_endian);
459
460         switch(pdut) {
461             case INVOKE:
462                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_Trailer, tvb, offCur, 1, bo_little_endian);
463                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
464                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
465                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
466
467                 proto_tree_add_item(wtp_tree, hf_wtp_header_Inv_version , tvb, offCur + 3, 1, bo_little_endian);
468                 proto_tree_add_item(wtp_tree, hf_wtp_header_Inv_flag_TIDNew, tvb, offCur + 3, 1, bo_little_endian);
469                 proto_tree_add_item(wtp_tree, hf_wtp_header_Inv_flag_UP, tvb, offCur + 3, 1, bo_little_endian);
470                 proto_tree_add_item(wtp_tree, hf_wtp_header_Inv_Reserved, tvb, offCur + 3, 1, bo_little_endian);
471                 proto_tree_add_item(wtp_tree, hf_wtp_header_Inv_TransactionClass, tvb, offCur + 3, 1, bo_little_endian);
472                 break;
473
474             case RESULT:
475                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_Trailer, tvb, offCur, 1, bo_little_endian);
476                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
477                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
478                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
479                 break;
480
481             case ACK:
482                 proto_tree_add_item(wtp_tree, hf_wtp_header_Ack_flag_TVETOK, tvb, offCur, 1, bo_big_endian);
483
484                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
485                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
486                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
487                 break;
488
489             case ABORT:
490                 abortType = tvb_get_guint8 (tvb, offCur) & 0x07;
491                 proto_tree_add_item(wtp_tree, hf_wtp_header_Abort_type , tvb, offCur , 1, bo_little_endian);
492                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
493                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
494
495                 if (abortType == PROVIDER)
496                 {
497                     proto_tree_add_item( wtp_tree, hf_wtp_header_Abort_reason_provider , tvb, offCur + 3 , 1, bo_little_endian);
498                 }
499                 else if (abortType == USER)
500                 {
501                     proto_tree_add_item(wtp_tree, hf_wtp_header_Abort_reason_user , tvb, offCur + 3 , 1, bo_little_endian);
502                 }
503                 break;
504
505             case SEGMENTED_INVOKE:
506                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_Trailer, tvb, offCur, 1, bo_little_endian);
507                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
508                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
509                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
510
511                 proto_tree_add_item(wtp_tree, hf_wtp_header_sequence_number , tvb, offCur + 3, 1, bo_little_endian);
512                 break;
513
514             case SEGMENTED_RESULT:
515                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_Trailer, tvb, offCur, 1, bo_little_endian);
516                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
517                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
518                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
519
520                 proto_tree_add_item(wtp_tree, hf_wtp_header_sequence_number , tvb, offCur + 3, 1, bo_little_endian);
521                 break;
522
523             case NEGATIVE_ACK:
524                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
525                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
526                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
527
528                 proto_tree_add_item(wtp_tree, hf_wtp_header_missing_packets , tvb, offCur + 3, 1, bo_little_endian);
529                 /* Iterate through missing packets */
530                 for (i = 0; i < numMissing; i++)
531                 {
532                     proto_tree_add_item(wtp_tree, hf_wtp_header_sequence_number, tvb, offCur + 4 + i, 1, bo_little_endian);
533                 }
534                 break;
535
536             default:
537                 break;
538         };
539     } else { /* Tree = closed */
540 #ifdef DEBUG
541         fprintf(stderr, "dissect_wtp: (4) tree was %p\n", tree);
542 #endif
543     }
544         /* Process the varialbe part */
545         if (fCon) {                     /* Now, analyze variable part   */
546             unsigned char        tCon;
547             unsigned char        tByte;
548             unsigned char        tpiLen;
549             tvbuff_t            *tmp_tvb;
550
551             vHeader = 0;                /* Start scan all over  */
552
553             do {
554                 tByte = tvb_get_guint8(tvb, offCur + cbHeader + vHeader);
555                 tCon = tByte & 0x80;
556                 if (tByte & 0x04)       /* Long TPI     */
557                     tpiLen = 2 + tvb_get_guint8(tvb,
558                                             offCur + cbHeader + vHeader + 1);
559                 else
560                     tpiLen = 1 + (tByte & 0x03);
561                 if (tree)
562                 {
563                 tmp_tvb = tvb_new_subset(tvb, offCur + cbHeader + vHeader,
564                                         tpiLen, tpiLen);
565                 wtp_handle_tpi(wtp_tree, tmp_tvb);
566                 }
567                 vHeader += tpiLen;
568             } while (tCon);
569         } else {
570                 /* There is no variable part */
571         }       /* End of variable part of header */
572
573         /* Set the length of the WTP protocol part now we know the length of the
574          * fixed and variable WTP headers */
575         if (tree)
576         proto_item_set_len(ti, cbHeader + vHeader);
577
578 #ifdef DEBUG
579     fprintf( stderr, "dissect_wtp: cbHeader = %d\n", cbHeader );
580 #endif
581
582     /* Only update "Info" column when no data in this PDU will
583      * be handed off to a subsequent dissector.
584      */
585     if (check_col(pinfo->cinfo, COL_INFO) &&
586         ((tvb_length_remaining(tvb, offCur + cbHeader + vHeader) <= 0) ||
587          (pdut == ACK) || (pdut==NEGATIVE_ACK) || (pdut==ABORT)) ) {
588 #ifdef DEBUG
589         fprintf(stderr, "dissect_wtp: (6) About to set info_col header to %s\n", szInfo->str);
590 #endif
591         col_append_str(pinfo->cinfo, COL_INFO, szInfo->str);
592     };
593
594         /*
595      * Any remaining data ought to be WSP data (if not WTP ACK, NACK
596      * or ABORT pdu), so hand off (defragmented) to the WSP dissector.
597      * Note that the last packet of a fragmented WTP message needn't
598      * contain any data, so we allow payloadless packets to be
599      * reassembled.  (XXX - does the reassembly code handle this
600      * for packets other than the last packet?)
601      */
602     if ((tvb_reported_length_remaining(tvb, offCur + cbHeader + vHeader) >= 0) &&
603         ! ((pdut==ACK) || (pdut==NEGATIVE_ACK) || (pdut==ABORT)))
604     {
605         int     dataOffset = offCur + cbHeader + vHeader;
606         gint    dataLen = tvb_reported_length_remaining(tvb, dataOffset);
607         gboolean save_fragmented;
608
609         if (((pdut == SEGMENTED_INVOKE) || (pdut == SEGMENTED_RESULT) ||
610             (((pdut == INVOKE) || (pdut == RESULT)) && (!fTTR))) &&
611             tvb_bytes_exist(tvb, dataOffset, dataLen))
612         {                                       /* 1st part of segment  */
613             save_fragmented = pinfo->fragmented;
614             pinfo->fragmented = TRUE;
615             fd_head = fragment_add_seq(tvb, dataOffset, pinfo, TID,
616                             wtp_fragment_table, psn, dataLen, !fTTR);
617             if (fd_head != NULL)                /* Reassembled  */
618             {
619                 /* Reassembly is complete; show the reassembled PDU */
620                 wsp_tvb = tvb_new_real_data(fd_head->data,
621                                             fd_head->len,
622                                             fd_head->len);
623                 tvb_set_child_real_data_tvbuff(tvb, wsp_tvb);
624                 add_new_data_source(pinfo, wsp_tvb,
625                                         "Reassembled WTP");
626                 pinfo->fragmented = FALSE;
627
628                 /* show all fragments */
629                 show_fragment_seq_tree(fd_head, &wtp_frag_items,
630                                         wtp_tree, pinfo, wsp_tvb);
631
632                 if (pinfo->fd->num == fd_head->reassembled_in) {
633                         /* Reassembled in this packet */
634                         call_dissector(wsp_handle, wsp_tvb, pinfo, tree);
635                 } else {
636                         /* Reassembled in another packet */
637                         if (check_col(pinfo->cinfo, COL_INFO))
638                                 /* Won't call WSP so display */
639                         col_append_str(pinfo->cinfo, COL_INFO, szInfo->str);
640                         if (tree != NULL)
641                             proto_tree_add_text(wtp_tree, tvb, dataOffset, -1, "Payload");
642                         proto_tree_add_uint(wtp_tree, hf_wtp_reassembled_in,
643                                         tvb, 0, 0, fd_head->reassembled_in);
644                 }
645             }
646             else
647             {
648                 /* Reassembly isn't complete; just show the fragment */
649                 if (check_col(pinfo->cinfo, COL_INFO))
650                         /* Won't call WSP so display */
651                     col_append_str(pinfo->cinfo, COL_INFO, szInfo->str);
652                 if (tree != NULL)
653                     proto_tree_add_text(wtp_tree, tvb, dataOffset, -1, "Payload");
654             }
655             pinfo->fragmented = save_fragmented;
656         }
657         else
658         {
659             /*
660              * Normal packet, or not all the fragment data is available;
661              * call next dissector, unless this is a segment of a
662              * segmented invoke or result and isn't the first segment.
663              */
664             if ((pdut == SEGMENTED_INVOKE || pdut == SEGMENTED_RESULT) &&
665                         psn != 0) {
666                 /*
667                  * This is a segmented invoke or result, and not the first
668                  * segment; just show it as a segmented invoke or result,
669                  * don't try to dissect its payload.
670                  */
671                 if (check_col(pinfo->cinfo, COL_INFO))
672                     col_add_fstr(pinfo->cinfo, COL_INFO,
673                                  "WTP %s (%u)",
674                                  (pdut == SEGMENTED_INVOKE ?
675                                   "Segmented Invoke" : "Segmented Result"),
676                                  psn);
677                 if (tree != NULL)
678                     proto_tree_add_text(wtp_tree, tvb, dataOffset, -1, "Payload");
679             } else {
680                 wsp_tvb = tvb_new_subset(tvb, dataOffset, -1, -1);
681                 call_dissector(wsp_handle, wsp_tvb, pinfo, tree);
682             }
683         }
684     }
685 }
686
687 /*
688  * Called directly from UDP.
689  * Put "WTP+WSP" into the "Protocol" column.
690  */
691 static void
692 dissect_wtp_fromudp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
693 {
694     if (check_col(pinfo->cinfo, COL_PROTOCOL))
695         col_set_str(pinfo->cinfo, COL_PROTOCOL, "WTP+WSP" );
696     if (check_col(pinfo->cinfo, COL_INFO))
697         col_clear(pinfo->cinfo, COL_INFO);
698
699     dissect_wtp_common(tvb, pinfo, tree);
700 }
701
702 /*
703  * Called from a higher-level WAP dissector, presumably WTLS.
704  * Put "WTLS+WSP+WTP" to the "Protocol" column.
705  *
706  * XXX - is this supposed to be called from WTLS?  If so, we're not
707  * calling it....
708  *
709  * XXX - can this be called from any other dissector?
710  */
711 static void
712 dissect_wtp_fromwtls(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
713 {
714     if (check_col(pinfo->cinfo, COL_PROTOCOL))
715         col_set_str(pinfo->cinfo, COL_PROTOCOL, "WTLS+WTP+WSP" );
716     if (check_col(pinfo->cinfo, COL_INFO))
717         col_clear(pinfo->cinfo, COL_INFO);
718
719     dissect_wtp_common(tvb, pinfo, tree);
720 }
721
722 /* Register the protocol with Ethereal */
723 void
724 proto_register_wtp(void)
725 {
726
727     /* Setup list of header fields */
728     static hf_register_info hf[] = {
729         { &hf_wtp_header_sub_pdu_size,
730             {   "Sub PDU size",
731                 "wtp.sub_pdu_size",
732                 FT_UINT16, BASE_DEC, NULL, 0x0,
733                 "Size of Sub-PDU (bytes)", HFILL
734             }
735         },
736         { &hf_wtp_header_flag_continue,
737             {   "Continue Flag",
738                 "wtp.continue_flag",
739                 FT_BOOLEAN, 8, TFS( &continue_truth ), 0x80,
740                 "Continue Flag", HFILL
741             }
742         },
743         { &hf_wtp_header_pdu_type,
744             {   "PDU Type",
745                 "wtp.pdu_type",
746                 FT_UINT8, BASE_HEX, VALS( vals_wtp_pdu_type ), 0x78,
747                 "PDU Type", HFILL
748             }
749         },
750         { &hf_wtp_header_flag_Trailer,
751             {   "Trailer Flags",
752                 "wtp.trailer_flags",
753                 FT_UINT8, BASE_HEX, VALS( vals_transaction_trailer ), 0x06,
754                 "Trailer Flags", HFILL
755             }
756         },
757         { &hf_wtp_header_flag_RID,
758             {   "Re-transmission Indicator",
759                 "wtp.RID",
760                 FT_BOOLEAN, 8, TFS( &RID_truth ), 0x01,
761                 "Re-transmission Indicator", HFILL
762             }
763         },
764         { &hf_wtp_header_flag_TID_response,
765             {   "TID Response",
766                 "wtp.TID.response",
767                 FT_BOOLEAN, 16, TFS( &tid_response_truth ), 0x8000,
768                 "TID Response", HFILL
769             }
770         },
771         { &hf_wtp_header_flag_TID,
772             {   "Transaction ID",
773                 "wtp.TID",
774                 FT_UINT16, BASE_HEX, NULL, 0x7FFF,
775                 "Transaction ID", HFILL
776             }
777         },
778         { &hf_wtp_header_Inv_version,
779             {   "Version",
780                 "wtp.header.version",
781                 FT_UINT8, BASE_HEX, VALS( vals_version ), 0xC0,
782                 "Version", HFILL
783             }
784         },
785         { &hf_wtp_header_Inv_flag_TIDNew,
786             {   "TIDNew",
787                 "wtp.header.TIDNew",
788                 FT_BOOLEAN, 8, TFS( &TIDNew_truth ), 0x20,
789                 "TIDNew", HFILL
790             }
791         },
792         { &hf_wtp_header_Inv_flag_UP,
793             {   "U/P flag",
794                 "wtp.header.UP",
795                 FT_BOOLEAN, 8, TFS( &UP_truth ), 0x10,
796                 "U/P Flag", HFILL
797             }
798         },
799         { &hf_wtp_header_Inv_Reserved,
800             {   "Reserved",
801                 "wtp.inv.reserved",
802                 FT_UINT8, BASE_HEX, NULL, 0x0C,
803                 "Reserved", HFILL
804             }
805         },
806         { &hf_wtp_header_Inv_TransactionClass,
807             {   "Transaction Class",
808                 "wtp.inv.transaction_class",
809                 FT_UINT8, BASE_HEX, VALS( vals_transaction_classes ), 0x03,
810                 "Transaction Class", HFILL
811             }
812         },
813         { &hf_wtp_header_Ack_flag_TVETOK,
814             {   "Tve/Tok flag",
815                 "wtp.ack.tvetok",
816                 FT_BOOLEAN, 8, TFS( &TVETOK_truth ), 0x04,
817                 "Tve/Tok flag", HFILL
818             }
819         },
820         { &hf_wtp_header_Abort_type,
821             {   "Abort Type",
822                 "wtp.abort.type",
823                 FT_UINT8, BASE_HEX, VALS ( vals_abort_type ), 0x07,
824                 "Abort Type", HFILL
825             }
826         },
827         { &hf_wtp_header_Abort_reason_provider,
828             {   "Abort Reason",
829                 "wtp.abort.reason.provider",
830                 FT_UINT8, BASE_HEX, VALS ( vals_abort_reason_provider ), 0x00,
831                 "Abort Reason", HFILL
832             }
833         },
834         /* Assume WSP is the user and use its reason codes */
835         { &hf_wtp_header_Abort_reason_user,
836             {   "Abort Reason",
837                 "wtp.abort.reason.user",
838                 FT_UINT8, BASE_HEX, VALS ( vals_wsp_reason_codes ), 0x00,
839                 "Abort Reason", HFILL
840             }
841         },
842         { &hf_wtp_header_sequence_number,
843             {   "Packet Sequence Number",
844                 "wtp.header.sequence",
845                 FT_UINT8, BASE_DEC, NULL, 0x00,
846                 "Packet Sequence Number", HFILL
847             }
848         },
849         { &hf_wtp_header_missing_packets,
850             {   "Missing Packets",
851                 "wtp.header.missing_packets",
852                 FT_UINT8, BASE_DEC, NULL, 0x00,
853                 "Missing Packets", HFILL
854             }
855         },
856         { &hf_wtp_header_variable_part,
857             {   "Header: Variable part",
858                 "wtp.header_variable_part",
859                 FT_BYTES, BASE_HEX, NULL, 0x0,
860                 "Variable part of the header", HFILL
861             }
862         },
863         { &hf_wtp_data,
864             {   "Data",
865                 "wtp.header_data",
866                 FT_BYTES, BASE_HEX, NULL, 0x0,
867                 "Data", HFILL
868             }
869         },
870         { &hf_wtp_tpi_type,
871             {   "TPI",
872                 "wtp.tpi",
873                 FT_UINT8, BASE_HEX, VALS(vals_tpi_type), 0x00,
874                 "Identification of the Transport Information Item", HFILL
875             }
876         },
877         { &hf_wtp_tpi_psn,
878             {   "Packet sequence number",
879                 "wtp.tpi.psn",
880                 FT_UINT8, BASE_DEC, NULL, 0x00,
881                 "Sequence number of this packet", HFILL
882             }
883         },
884         { &hf_wtp_tpi_opt,
885             {   "Option",
886                 "wtp.tpi.opt",
887                 FT_UINT8, BASE_HEX, VALS(vals_tpi_opt), 0x00,
888                 "The given option for this TPI", HFILL
889             }
890         },
891         { &hf_wtp_tpi_optval,
892             {   "Option Value",
893                 "wtp.tpi.opt.val",
894                 FT_NONE, BASE_NONE, NULL, 0x00,
895                 "The value that is supplied with this option", HFILL
896             }
897         },
898         { &hf_wtp_tpi_info,
899             {   "Information",
900                 "wtp.tpi.info",
901                 FT_NONE, BASE_NONE, NULL, 0x00,
902                 "The information being send by this TPI", HFILL
903             }
904         },
905
906         /* Fragment fields */
907         { &hf_wtp_fragment_overlap,
908             {   "Fragment overlap",
909                 "wtp.fragment.overlap",
910                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
911                 "Fragment overlaps with other fragments", HFILL
912             }
913         },
914         { &hf_wtp_fragment_overlap_conflict,
915             {   "Conflicting data in fragment overlap",
916                 "wtp.fragment.overlap.conflict",
917                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
918                 "Overlapping fragments contained conflicting data", HFILL
919             }
920         },
921         { &hf_wtp_fragment_multiple_tails,
922             {   "Multiple tail fragments found",
923                 "wtp.fragment.multipletails",
924                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
925                 "Several tails were found when defragmenting the packet", HFILL
926             }
927         },
928         { &hf_wtp_fragment_too_long_fragment,
929             {   "Fragment too long",
930                 "wtp.fragment.toolongfragment",
931                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
932                 "Fragment contained data past end of packet", HFILL
933             }
934         },
935         { &hf_wtp_fragment_error,
936             {   "Defragmentation error",
937                 "wtp.fragment.error",
938                 FT_FRAMENUM, BASE_NONE, NULL, 0x0,
939                 "Defragmentation error due to illegal fragments", HFILL
940             }
941         },
942         { &hf_wtp_reassembled_in,
943             {   "Reassembled in",
944                 "wtp.reassembled.in",
945                 FT_FRAMENUM, BASE_NONE, NULL, 0x0,
946                 "WTP fragments are reassembled in the given packet", HFILL
947             }
948         },
949         { &hf_wtp_fragment,
950             {   "WTP Fragment",
951                 "wtp.fragment",
952                 FT_FRAMENUM, BASE_NONE, NULL, 0x0,
953                 "WTP Fragment", HFILL
954             }
955         },
956         { &hf_wtp_fragments,
957             {   "WTP Fragments",
958                 "wtp.fragments",
959                 FT_NONE, BASE_NONE, NULL, 0x0,
960                 "WTP Fragments", HFILL
961             }
962         },
963     };
964
965     /* Setup protocol subtree array */
966     static gint *ett[] = {
967         &ett_wtp,
968         &ett_wtp_sub_pdu_tree,
969         &ett_header,
970         &ett_tpilist,
971         &ett_wsp_fragments,
972         &ett_wtp_fragment,
973     };
974
975     /* Register the protocol name and description */
976     proto_wtp = proto_register_protocol(
977         "Wireless Transaction Protocol",   /* protocol name for use by ethereal */
978         "WTP",                             /* short version of name */
979         "wtp"                      /* Abbreviated protocol name, should Match IANA
980                                             < URL:http://www.isi.edu/in-notes/iana/assignments/port-numbers/ >
981                                             */
982     );
983
984     /* Required calls to register the header fields and subtrees used */
985     proto_register_field_array(proto_wtp, hf, array_length(hf));
986     proto_register_subtree_array(ett, array_length(ett));
987
988     register_dissector("wtp-wtls", dissect_wtp_fromwtls, proto_wtp);
989     register_dissector("wtp-udp", dissect_wtp_fromudp, proto_wtp);
990     register_init_routine(wtp_defragment_init);
991 }
992
993 void
994 proto_reg_handoff_wtp(void)
995 {
996     dissector_handle_t wtp_fromudp_handle;
997
998     /*
999      * Get a handle for the connection-oriented WSP dissector - if WTP
1000      * PDUs have data, it is WSP.
1001      */
1002     wsp_handle = find_dissector("wsp-co");
1003
1004     wtp_fromudp_handle = find_dissector("wtp-udp");
1005     dissector_add("udp.port", UDP_PORT_WTP_WSP, wtp_fromudp_handle);
1006     dissector_add("smpp.gsm-sms.udh.port", UDP_PORT_WTP_WSP, wtp_fromudp_handle);
1007 }