Indicate where BACnet MS/TP is specified.
[metze/wireshark/wip.git] / epan / dissectors / packet-frame.c
1 /* packet-frame.c
2  *
3  * Top-most dissector. Decides dissector based on Wiretap Encapsulation Type.
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 2000 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #ifdef _MSC_VER
31 #include <windows.h>
32 #endif
33
34
35 #include <glib.h>
36 #include <epan/packet.h>
37 #include <epan/timestamp.h>
38 #include "packet-frame.h"
39 #include <epan/prefs.h>
40 #include <epan/tap.h>
41 #include <epan/expert.h>
42 #include <epan/crypt/md5.h>
43
44 #include "color.h"
45 #include "color_filters.h"
46
47 int proto_frame = -1;
48 int proto_pkt_comment = -1;
49 int hf_frame_arrival_time = -1;
50 int hf_frame_shift_offset = -1;
51 int hf_frame_arrival_time_epoch = -1;
52 static int hf_frame_time_invalid = -1;
53 static int hf_frame_time_delta = -1;
54 static int hf_frame_time_delta_displayed = -1;
55 static int hf_frame_time_relative = -1;
56 static int hf_frame_time_reference = -1;
57 int hf_frame_number = -1;
58 int hf_frame_len = -1;
59 int hf_frame_capture_len = -1;
60 static int hf_frame_p2p_dir = -1;
61 static int hf_frame_file_off = -1;
62 static int hf_frame_md5_hash = -1;
63 static int hf_frame_marked = -1;
64 static int hf_frame_ignored = -1;
65 static int hf_link_number = -1;
66 static int hf_frame_protocols = -1;
67 static int hf_frame_color_filter_name = -1;
68 static int hf_frame_color_filter_text = -1;
69 static int hf_frame_interface_id = -1;
70 static int hf_comments_text = -1;
71
72 static int proto_short = -1;
73 int proto_malformed = -1;
74 static int proto_unreassembled = -1;
75
76 static gint ett_frame = -1;
77 static gint ett_comments = -1;
78
79 static int frame_tap = -1;
80
81 static dissector_handle_t data_handle;
82 static dissector_handle_t docsis_handle;
83
84 /* Preferences */
85 static gboolean show_file_off = FALSE;
86 static gboolean force_docsis_encap = FALSE;
87 static gboolean generate_md5_hash = FALSE;
88 static gboolean generate_epoch_time = TRUE;
89 static gboolean generate_bits_field = TRUE;
90
91 static const value_string p2p_dirs[] = {
92         { P2P_DIR_UNKNOWN, "Unknown" },
93         { P2P_DIR_SENT, "Sent" },
94         { P2P_DIR_RECV, "Received" },
95         { 0, NULL }
96 };
97
98 dissector_table_t wtap_encap_dissector_table;
99
100 static GSList *frame_end_routines = NULL;
101
102 /*
103  * Routine used to register frame end routine.  The routine should only
104  * be registered when the dissector is used in the frame, not in the
105  * proto_register_XXX function.
106  */
107 void
108 register_frame_end_routine(void (*func)(void))
109 {
110         frame_end_routines = g_slist_append(frame_end_routines, (gpointer)func);
111 }
112
113 typedef void (*void_func_t)(void);
114
115 static void
116 call_frame_end_routine(gpointer routine, gpointer dummy _U_)
117 {
118         void_func_t func = (void_func_t)routine;
119         (*func)();
120 }
121
122 static void
123 dissect_frame(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
124 {
125         proto_item      *volatile ti = NULL, *comment_item;
126         guint           cap_len = 0, frame_len = 0;
127         proto_tree      *volatile tree;
128         proto_tree  *comments_tree;
129     proto_item  *item;
130         const gchar *cap_plurality, *frame_plurality;
131
132         tree=parent_tree;
133
134         pinfo->current_proto = "Frame";
135
136         if (pinfo->pseudo_header != NULL) {
137                 switch (pinfo->fd->lnk_t) {
138
139                 case WTAP_ENCAP_WFLEET_HDLC:
140                 case WTAP_ENCAP_CHDLC_WITH_PHDR:
141                 case WTAP_ENCAP_PPP_WITH_PHDR:
142                 case WTAP_ENCAP_SDLC:
143                 case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
144                         pinfo->p2p_dir = pinfo->pseudo_header->p2p.sent ?
145                             P2P_DIR_SENT : P2P_DIR_RECV;
146                         break;
147
148                 case WTAP_ENCAP_BLUETOOTH_HCI:
149                         pinfo->p2p_dir = pinfo->pseudo_header->bthci.sent;
150                         break;
151
152                 case WTAP_ENCAP_LAPB:
153                 case WTAP_ENCAP_FRELAY_WITH_PHDR:
154                         pinfo->p2p_dir =
155                             (pinfo->pseudo_header->x25.flags & FROM_DCE) ?
156                             P2P_DIR_RECV : P2P_DIR_SENT;
157                         break;
158
159                 case WTAP_ENCAP_ISDN:
160                 case WTAP_ENCAP_V5_EF:
161                 case WTAP_ENCAP_DPNSS:
162                 case WTAP_ENCAP_BACNET_MS_TP_WITH_PHDR:
163                         pinfo->p2p_dir = pinfo->pseudo_header->isdn.uton ?
164                             P2P_DIR_SENT : P2P_DIR_RECV;
165                         break;
166
167                 case WTAP_ENCAP_LINUX_LAPD:
168                         pinfo->p2p_dir = (pinfo->pseudo_header->lapd.pkttype == 3 ||
169                                 pinfo->pseudo_header->lapd.pkttype == 4) ?
170                                 P2P_DIR_SENT : P2P_DIR_RECV;
171                         break;
172
173                 case WTAP_ENCAP_MTP2_WITH_PHDR:
174                         pinfo->p2p_dir = pinfo->pseudo_header->mtp2.sent ?
175                             P2P_DIR_SENT : P2P_DIR_RECV;
176                         pinfo->link_number  = pinfo->pseudo_header->mtp2.link_number;
177                         pinfo->annex_a_used = pinfo->pseudo_header->mtp2.annex_a_used;
178                         break;
179
180                 case WTAP_ENCAP_GSM_UM:
181                         pinfo->p2p_dir = pinfo->pseudo_header->gsm_um.uplink ?
182                             P2P_DIR_SENT : P2P_DIR_RECV;
183                         break;
184
185                 }
186         }
187
188         if(pinfo->fd->opt_comment){
189                 item = proto_tree_add_item(tree, proto_pkt_comment, tvb, 0, -1, ENC_NA);
190                 comments_tree = proto_item_add_subtree(item, ett_comments);
191                 comment_item = proto_tree_add_string_format(comments_tree, hf_comments_text, tvb, 0, -1,
192                                                                            pinfo->fd->opt_comment, "%s",
193                                                                            pinfo->fd->opt_comment);
194                 expert_add_info_format(pinfo, comment_item, PI_COMMENTS_GROUP, PI_COMMENT,
195                                                                "%s",  pinfo->fd->opt_comment);
196
197
198         }
199
200         /* if FRAME is not referenced from any filters we dont need to worry about
201            generating any tree items.  */
202         if(!proto_field_is_referenced(tree, proto_frame)) {
203                 tree=NULL;
204                 if(pinfo->fd->flags.has_ts) {
205                         if(pinfo->fd->abs_ts.nsecs < 0 || pinfo->fd->abs_ts.nsecs >= 1000000000)
206                                 expert_add_info_format(pinfo, NULL, PI_MALFORMED, PI_WARN,
207                                                        "Arrival Time: Fractional second out of range (0-1000000000)");
208                 }
209         } else {
210                 proto_tree      *fh_tree;
211                 gboolean old_visible;
212
213                 /* Put in frame header information. */
214                 cap_len = tvb_length(tvb);
215                 frame_len = tvb_reported_length(tvb);
216
217                 cap_plurality = plurality(cap_len, "", "s");
218                 frame_plurality = plurality(frame_len, "", "s");
219
220                 ti = proto_tree_add_protocol_format(tree, proto_frame, tvb, 0, -1,
221                     "Frame %u: %u byte%s on wire",
222                     pinfo->fd->num, frame_len, frame_plurality);
223                 if (generate_bits_field)
224                         proto_item_append_text(ti, " (%u bits)", frame_len * 8);
225                 proto_item_append_text(ti, ", %u byte%s captured",
226                     cap_len, cap_plurality);
227                 if (generate_bits_field) {
228                         proto_item_append_text(ti, " (%u bits)",
229                             cap_len * 8);
230                 }
231                 if (pinfo->fd->flags.has_if_id) {
232                         proto_item_append_text(ti, " on interface %u",
233                             pinfo->fd->interface_id);
234                 }
235
236                 fh_tree = proto_item_add_subtree(ti, ett_frame);
237
238                 if (pinfo->fd->flags.has_if_id)
239                         proto_tree_add_uint(fh_tree, hf_frame_interface_id, tvb, 0, 0, pinfo->fd->interface_id);
240
241                 if (pinfo->fd->flags.has_ts) {
242                         proto_tree_add_time(fh_tree, hf_frame_arrival_time, tvb,
243                                             0, 0, &(pinfo->fd->abs_ts));
244                         if(pinfo->fd->abs_ts.nsecs < 0 || pinfo->fd->abs_ts.nsecs >= 1000000000) {
245                                 item = proto_tree_add_none_format(fh_tree, hf_frame_time_invalid, tvb,
246                                                                   0, 0, "Arrival Time: Fractional second %09ld is invalid, the valid range is 0-1000000000", (long) pinfo->fd->abs_ts.nsecs);
247                                 PROTO_ITEM_SET_GENERATED(item);
248                                 expert_add_info_format(pinfo, item, PI_MALFORMED, PI_WARN, "Arrival Time: Fractional second out of range (0-1000000000)");
249                         }
250                         item = proto_tree_add_time(fh_tree, hf_frame_shift_offset, tvb,
251                                             0, 0, &(pinfo->fd->shift_offset));
252                         PROTO_ITEM_SET_GENERATED(item);
253
254                         if(generate_epoch_time) {
255                                 proto_tree_add_time(fh_tree, hf_frame_arrival_time_epoch, tvb,
256                                                     0, 0, &(pinfo->fd->abs_ts));
257                         }
258
259                         item = proto_tree_add_time(fh_tree, hf_frame_time_delta, tvb,
260                                                    0, 0, &(pinfo->fd->del_cap_ts));
261                         PROTO_ITEM_SET_GENERATED(item);
262
263                         item = proto_tree_add_time(fh_tree, hf_frame_time_delta_displayed, tvb,
264                                                    0, 0, &(pinfo->fd->del_dis_ts));
265                         PROTO_ITEM_SET_GENERATED(item);
266
267                         item = proto_tree_add_time(fh_tree, hf_frame_time_relative, tvb,
268                                                    0, 0, &(pinfo->fd->rel_ts));
269                         PROTO_ITEM_SET_GENERATED(item);
270
271                         if(pinfo->fd->flags.ref_time){
272                                 ti = proto_tree_add_item(fh_tree, hf_frame_time_reference, tvb, 0, 0, ENC_NA);
273                                 PROTO_ITEM_SET_GENERATED(ti);
274                         }
275                 }
276
277                 proto_tree_add_uint(fh_tree, hf_frame_number, tvb,
278                                     0, 0, pinfo->fd->num);
279
280                 proto_tree_add_uint_format(fh_tree, hf_frame_len, tvb,
281                                            0, 0, frame_len, "Frame Length: %u byte%s (%u bits)",
282                                            frame_len, frame_plurality, frame_len * 8);
283
284                 proto_tree_add_uint_format(fh_tree, hf_frame_capture_len, tvb,
285                                            0, 0, cap_len, "Capture Length: %u byte%s (%u bits)",
286                                            cap_len, cap_plurality, cap_len * 8);
287
288                 if (generate_md5_hash) {
289                         const guint8 *cp;
290                         md5_state_t md_ctx;
291                         md5_byte_t digest[16];
292                         gchar *digest_string;
293
294                         cp = tvb_get_ptr(tvb, 0, cap_len);
295
296                         md5_init(&md_ctx);
297                         md5_append(&md_ctx, cp, cap_len);
298                         md5_finish(&md_ctx, digest);
299
300                         digest_string = bytestring_to_str(digest, 16, '\0');
301                         ti = proto_tree_add_string(fh_tree, hf_frame_md5_hash, tvb, 0, 0, digest_string);
302                         PROTO_ITEM_SET_GENERATED(ti);
303                 }
304
305                 ti = proto_tree_add_boolean(fh_tree, hf_frame_marked, tvb, 0, 0,pinfo->fd->flags.marked);
306                 PROTO_ITEM_SET_GENERATED(ti);
307
308                 ti = proto_tree_add_boolean(fh_tree, hf_frame_ignored, tvb, 0, 0,pinfo->fd->flags.ignored);
309                 PROTO_ITEM_SET_GENERATED(ti);
310
311                 if(proto_field_is_referenced(tree, hf_frame_protocols)) {
312                         /* we are going to be using proto_item_append_string() on
313                          * hf_frame_protocols, and we must therefore disable the
314                          * TRY_TO_FAKE_THIS_ITEM() optimisation for the tree by
315                          * setting it as visible.
316                          *
317                          * See proto.h for details.
318                          */
319                         old_visible = proto_tree_set_visible(fh_tree, TRUE);
320                         ti = proto_tree_add_string(fh_tree, hf_frame_protocols, tvb, 0, 0, "");
321                         PROTO_ITEM_SET_GENERATED(ti);
322                         proto_tree_set_visible(fh_tree, old_visible);
323
324                         pinfo->layer_names = g_string_new("");
325                 }
326                 else
327                         pinfo->layer_names = NULL;
328
329                 /* Check for existences of P2P pseudo header */
330                 if (pinfo->p2p_dir != P2P_DIR_UNKNOWN) {
331                         proto_tree_add_int(fh_tree, hf_frame_p2p_dir, tvb,
332                                            0, 0, pinfo->p2p_dir);
333                 }
334
335                 /* Check for existences of MTP2 link number */
336                 if ((pinfo->pseudo_header != NULL ) && (pinfo->fd->lnk_t == WTAP_ENCAP_MTP2_WITH_PHDR)) {
337                         proto_tree_add_uint(fh_tree, hf_link_number, tvb,
338                                             0, 0, pinfo->link_number);
339                 }
340
341                 if (show_file_off) {
342                         proto_tree_add_int64_format(fh_tree, hf_frame_file_off, tvb,
343                                                     0, 0, pinfo->fd->file_off,
344                                                     "File Offset: %" G_GINT64_MODIFIER "d (0x%" G_GINT64_MODIFIER "x)",
345                                                     pinfo->fd->file_off, pinfo->fd->file_off);
346                 }
347
348                 if(pinfo->fd->color_filter != NULL) {
349                         const color_filter_t *color_filter = pinfo->fd->color_filter;
350                         item = proto_tree_add_string(fh_tree, hf_frame_color_filter_name, tvb,
351                                                      0, 0, color_filter->filter_name);
352                         PROTO_ITEM_SET_GENERATED(item);
353                         item = proto_tree_add_string(fh_tree, hf_frame_color_filter_text, tvb,
354                                                      0, 0, color_filter->filter_text);
355                         PROTO_ITEM_SET_GENERATED(item);
356                 }
357         }
358
359         if (pinfo->fd->flags.ignored) {
360                 /* Ignored package, stop handling here */
361                 col_set_str(pinfo->cinfo, COL_INFO, "<Ignored>");
362                 proto_tree_add_text (tree, tvb, 0, -1, "This frame is marked as ignored");
363                 return;
364         }
365
366         /* Portable Exception Handling to trap Wireshark specific exceptions like BoundsError exceptions */
367         TRY {
368 #ifdef _MSC_VER
369                 /* Win32: Visual-C Structured Exception Handling (SEH) to trap hardware exceptions like memory access violations */
370                 /* (a running debugger will be called before the except part below) */
371                 __try {
372 #endif
373                         if ((force_docsis_encap) && (docsis_handle)) {
374                                 call_dissector(docsis_handle, tvb, pinfo, parent_tree);
375                         } else {
376                                 if (!dissector_try_uint(wtap_encap_dissector_table, pinfo->fd->lnk_t,
377                                                         tvb, pinfo, parent_tree)) {
378
379                                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "UNKNOWN");
380                                         col_add_fstr(pinfo->cinfo, COL_INFO, "WTAP_ENCAP = %d",
381                                                      pinfo->fd->lnk_t);
382                                         call_dissector(data_handle,tvb, pinfo, parent_tree);
383                                 }
384                         }
385 #ifdef _MSC_VER
386                 } __except(EXCEPTION_EXECUTE_HANDLER /* handle all exceptions */) {
387                         switch(GetExceptionCode()) {
388                         case(STATUS_ACCESS_VIOLATION):
389                                 show_exception(tvb, pinfo, parent_tree, DissectorError,
390                                                "STATUS_ACCESS_VIOLATION: dissector accessed an invalid memory address");
391                                 break;
392                         case(STATUS_INTEGER_DIVIDE_BY_ZERO):
393                                 show_exception(tvb, pinfo, parent_tree, DissectorError,
394                                                "STATUS_INTEGER_DIVIDE_BY_ZERO: dissector tried an integer division by zero");
395                                 break;
396                         case(STATUS_STACK_OVERFLOW):
397                                 show_exception(tvb, pinfo, parent_tree, DissectorError,
398                                                "STATUS_STACK_OVERFLOW: dissector overflowed the stack (e.g. endless loop)");
399                                 /* XXX - this will have probably corrupted the stack, which makes problems later in the exception code */
400                                 break;
401                                 /* XXX - add other hardware exception codes as required */
402                         default:
403                                 show_exception(tvb, pinfo, parent_tree, DissectorError,
404                                                g_strdup_printf("dissector caused an unknown exception: 0x%x", GetExceptionCode()));
405                         }
406                 }
407 #endif
408         }
409         CATCH(OutOfMemoryError) {
410                 RETHROW;
411         }
412         CATCH_ALL {
413                 show_exception(tvb, pinfo, parent_tree, EXCEPT_CODE, GET_MESSAGE);
414         }
415         ENDTRY;
416
417         if (tree && pinfo->layer_names) {
418                 proto_item_append_string(ti, pinfo->layer_names->str);
419                 g_string_free(pinfo->layer_names, TRUE);
420                 pinfo->layer_names = NULL;
421         }
422
423         /*  Call postdissectors if we have any (while trying to avoid another
424          *  TRY/CATCH)
425          */
426         if (have_postdissector()) {
427                 TRY {
428 #ifdef _MSC_VER
429                         /* Win32: Visual-C Structured Exception Handling (SEH) to trap hardware exceptions like memory access violations */
430                         /* (a running debugger will be called before the except part below) */
431                         __try {
432 #endif
433                                 call_all_postdissectors(tvb, pinfo, parent_tree);
434 #ifdef _MSC_VER
435                         } __except(EXCEPTION_EXECUTE_HANDLER /* handle all exceptions */) {
436                                 switch(GetExceptionCode()) {
437                                 case(STATUS_ACCESS_VIOLATION):
438                                         show_exception(tvb, pinfo, parent_tree, DissectorError,
439                                                        "STATUS_ACCESS_VIOLATION: dissector accessed an invalid memory address");
440                                         break;
441                                 case(STATUS_INTEGER_DIVIDE_BY_ZERO):
442                                         show_exception(tvb, pinfo, parent_tree, DissectorError,
443                                                        "STATUS_INTEGER_DIVIDE_BY_ZERO: dissector tried an integer division by zero");
444                                         break;
445                                 case(STATUS_STACK_OVERFLOW):
446                                         show_exception(tvb, pinfo, parent_tree, DissectorError,
447                                                        "STATUS_STACK_OVERFLOW: dissector overflowed the stack (e.g. endless loop)");
448                                         /* XXX - this will have probably corrupted the stack, which makes problems later in the exception code */
449                                         break;
450                                         /* XXX - add other hardware exception codes as required */
451                                 default:
452                                         show_exception(tvb, pinfo, parent_tree, DissectorError,
453                                                        g_strdup_printf("dissector caused an unknown exception: 0x%x", GetExceptionCode()));
454                                 }
455                         }
456 #endif
457                 }
458                 CATCH(OutOfMemoryError) {
459                         RETHROW;
460                 }
461                 CATCH_ALL {
462                         show_exception(tvb, pinfo, parent_tree, EXCEPT_CODE, GET_MESSAGE);
463                 }
464                 ENDTRY;
465         }
466
467         tap_queue_packet(frame_tap, pinfo, NULL);
468
469
470         if (frame_end_routines) {
471                 g_slist_foreach(frame_end_routines, &call_frame_end_routine, NULL);
472                 g_slist_free(frame_end_routines);
473                 frame_end_routines = NULL;
474         }
475 }
476
477 void
478 show_exception(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
479                unsigned long exception, const char *exception_message)
480 {
481         static const char dissector_error_nomsg[] =
482                 "Dissector writer didn't bother saying what the error was";
483         proto_item *item;
484
485
486         switch (exception) {
487
488         case ScsiBoundsError:
489                 col_append_str(pinfo->cinfo, COL_INFO, "[SCSI transfer limited due to allocation_length too small]");
490                 /*item =*/ proto_tree_add_protocol_format(tree, proto_short, tvb, 0, 0,
491                                 "SCSI transfer limited due to allocation_length too small: %s truncated]", pinfo->current_proto);
492                 /* Don't record ScsiBoundsError exceptions as expert events - they merely
493                  * reflect a normal SCSI condition.
494                  * (any case where it's caused by something else is a bug). */
495                 /* expert_add_info_format(pinfo, item, PI_MALFORMED, PI_ERROR, "Packet size limited");*/
496                 break;
497
498         case BoundsError:
499                 col_append_str(pinfo->cinfo, COL_INFO, "[Packet size limited during capture]");
500                 /*item =*/ proto_tree_add_protocol_format(tree, proto_short, tvb, 0, 0,
501                                 "[Packet size limited during capture: %s truncated]", pinfo->current_proto);
502                 /* Don't record BoundsError exceptions as expert events - they merely
503                  * reflect a capture done with a snapshot length too short to capture
504                  * all of the packet
505                  * (any case where it's caused by something else is a bug). */
506                 /* expert_add_info_format(pinfo, item, PI_MALFORMED, PI_ERROR, "Packet size limited");*/
507                 break;
508
509         case ReportedBoundsError:
510                 show_reported_bounds_error(tvb, pinfo, tree);
511                 break;
512
513         case DissectorError:
514                 col_append_fstr(pinfo->cinfo, COL_INFO,
515                     "[Dissector bug, protocol %s: %s]",
516                     pinfo->current_proto,
517                     exception_message == NULL ?
518                         dissector_error_nomsg : exception_message);
519                 item = proto_tree_add_protocol_format(tree, proto_malformed, tvb, 0, 0,
520                     "[Dissector bug, protocol %s: %s]",
521                     pinfo->current_proto,
522                     exception_message == NULL ?
523                         dissector_error_nomsg : exception_message);
524                 g_warning("Dissector bug, protocol %s, in packet %u: %s",
525                     pinfo->current_proto, pinfo->fd->num,
526                     exception_message == NULL ?
527                         dissector_error_nomsg : exception_message);
528                 expert_add_info_format(pinfo, item, PI_MALFORMED, PI_ERROR,
529                     "%s",
530                     exception_message == NULL ?
531                         dissector_error_nomsg : exception_message);
532                 break;
533
534         default:
535                 /* XXX - we want to know, if an unknown exception passed until here, don't we? */
536                 g_assert_not_reached();
537         }
538 }
539
540 void
541 show_reported_bounds_error(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
542 {
543         proto_item *item;
544
545         if (pinfo->fragmented) {
546                 /*
547                  * We were dissecting an unreassembled fragmented
548                  * packet when the exception was thrown, so the
549                  * problem isn't that the dissector expected
550                  * something but it wasn't in the packet, the
551                  * problem is that the dissector expected something
552                  * but it wasn't in the fragment we dissected.
553                  */
554                 col_append_fstr(pinfo->cinfo, COL_INFO,
555                     "[Unreassembled Packet%s] ",
556                     pinfo->noreassembly_reason);
557                 item = proto_tree_add_protocol_format(tree, proto_unreassembled,
558                     tvb, 0, 0, "[Unreassembled Packet%s: %s]",
559                     pinfo->noreassembly_reason, pinfo->current_proto);
560                 expert_add_info_format(pinfo, item, PI_REASSEMBLE, PI_WARN, "Unreassembled Packet (Exception occurred)");
561         } else {
562                 col_append_str(pinfo->cinfo, COL_INFO,
563                     "[Malformed Packet]");
564                 item = proto_tree_add_protocol_format(tree, proto_malformed,
565                     tvb, 0, 0, "[Malformed Packet: %s]", pinfo->current_proto);
566                 expert_add_info_format(pinfo, item, PI_MALFORMED, PI_ERROR, "Malformed Packet (Exception occurred)");
567         }
568 }
569
570 void
571 proto_register_frame(void)
572 {
573         static hf_register_info hf[] = {
574                 { &hf_frame_arrival_time,
575                 { "Arrival Time",               "frame.time", FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0x0,
576                         "Absolute time when this frame was captured", HFILL }},
577
578                 { &hf_frame_shift_offset,
579                 { "Time shift for this packet","frame.offset_shift", FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
580                         "Time shift applied to this packet", HFILL }},
581
582                 { &hf_frame_arrival_time_epoch,
583                 { "Epoch Time",                 "frame.time_epoch", FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
584                         "Epoch time when this frame was captured", HFILL }},
585
586                 { &hf_frame_time_invalid,
587                 { "Arrival Timestamp invalid",          "frame.time_invalid", FT_NONE, BASE_NONE, NULL, 0x0,
588                         "The timestamp from the capture is out of the valid range", HFILL }},
589
590                 { &hf_frame_time_delta,
591                 { "Time delta from previous captured frame",    "frame.time_delta", FT_RELATIVE_TIME, BASE_NONE, NULL,
592                         0x0,
593                         NULL, HFILL }},
594
595                 { &hf_frame_time_delta_displayed,
596                 { "Time delta from previous displayed frame",   "frame.time_delta_displayed", FT_RELATIVE_TIME, BASE_NONE, NULL,
597                         0x0,
598                         NULL, HFILL }},
599
600                 { &hf_frame_time_relative,
601                 { "Time since reference or first frame",        "frame.time_relative", FT_RELATIVE_TIME, BASE_NONE, NULL,
602                         0x0,
603                         "Time relative to time reference or first frame", HFILL }},
604
605                 { &hf_frame_time_reference,
606                 { "This is a Time Reference frame",     "frame.ref_time", FT_NONE, BASE_NONE, NULL, 0x0,
607                         "This frame is a Time Reference frame", HFILL }},
608
609                 { &hf_frame_number,
610                 { "Frame Number",               "frame.number", FT_UINT32, BASE_DEC, NULL, 0x0,
611                         NULL, HFILL }},
612
613                 { &hf_frame_len,
614                 { "Frame length on the wire",           "frame.len", FT_UINT32, BASE_DEC, NULL, 0x0,
615                         NULL, HFILL }},
616
617                 { &hf_frame_capture_len,
618                 { "Frame length stored into the capture file",  "frame.cap_len", FT_UINT32, BASE_DEC, NULL, 0x0,
619                         NULL, HFILL }},
620
621                 { &hf_frame_md5_hash,
622                 { "Frame MD5 Hash",     "frame.md5_hash", FT_STRING, BASE_NONE, NULL, 0x0,
623                         NULL, HFILL }},
624
625                 { &hf_frame_p2p_dir,
626                 { "Point-to-Point Direction",   "frame.p2p_dir", FT_INT8, BASE_DEC, VALS(p2p_dirs), 0x0,
627                         NULL, HFILL }},
628
629                 { &hf_link_number,
630                 { "Link Number",                "frame.link_nr", FT_UINT16, BASE_DEC, NULL, 0x0,
631                         NULL, HFILL }},
632
633                 { &hf_frame_file_off,
634                 { "File Offset",        "frame.file_off", FT_INT64, BASE_DEC, NULL, 0x0,
635                         NULL, HFILL }},
636
637                 { &hf_frame_marked,
638                 { "Frame is marked",    "frame.marked", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
639                         "Frame is marked in the GUI", HFILL }},
640
641                 { &hf_frame_ignored,
642                 { "Frame is ignored",   "frame.ignored", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
643                         "Frame is ignored by the dissectors", HFILL }},
644
645                 { &hf_frame_protocols,
646                 { "Protocols in frame", "frame.protocols", FT_STRING, BASE_NONE, NULL, 0x0,
647                         "Protocols carried by this frame", HFILL }},
648
649                 { &hf_frame_color_filter_name,
650                 { "Coloring Rule Name", "frame.coloring_rule.name", FT_STRING, BASE_NONE, NULL, 0x0,
651                         "The frame matched the coloring rule with this name", HFILL }},
652
653                 { &hf_frame_color_filter_text,
654                 { "Coloring Rule String", "frame.coloring_rule.string", FT_STRING, BASE_NONE, NULL, 0x0,
655                         "The frame matched this coloring rule string", HFILL }},
656
657                 { &hf_frame_interface_id,
658                 { "Interface id",               "frame.interface_id", FT_UINT32, BASE_DEC, NULL, 0x0,
659                         NULL, HFILL }},
660
661                 { &hf_comments_text,
662                 { "Comment", "comment", FT_STRING, BASE_NONE, NULL, 0x0,
663                         NULL, HFILL }},
664         };
665         static gint *ett[] = {
666                 &ett_frame,
667                 &ett_comments
668         };
669         module_t *frame_module;
670
671         wtap_encap_dissector_table = register_dissector_table("wtap_encap",
672             "Wiretap encapsulation type", FT_UINT32, BASE_DEC);
673
674         proto_frame = proto_register_protocol("Frame", "Frame", "frame");
675         proto_pkt_comment = proto_register_protocol("Packet comments", "Pkt_Comment", "pkt_comment");
676         proto_register_field_array(proto_frame, hf, array_length(hf));
677         proto_register_subtree_array(ett, array_length(ett));
678         register_dissector("frame",dissect_frame,proto_frame);
679
680         /* You can't disable dissection of "Frame", as that would be
681            tantamount to not doing any dissection whatsoever. */
682         proto_set_cant_toggle(proto_frame);
683
684         proto_short = proto_register_protocol("Short Frame", "Short frame", "short");
685         proto_malformed = proto_register_protocol("Malformed Packet",
686             "Malformed packet", "malformed");
687         proto_unreassembled = proto_register_protocol(
688             "Unreassembled Fragmented Packet",
689             "Unreassembled fragmented packet", "unreassembled");
690
691         /* "Short Frame", "Malformed Packet", and "Unreassembled Fragmented
692            Packet" aren't really protocols, they're error indications;
693            disabling them makes no sense. */
694         proto_set_cant_toggle(proto_short);
695         proto_set_cant_toggle(proto_malformed);
696         proto_set_cant_toggle(proto_unreassembled);
697
698         /* Our preferences */
699         frame_module = prefs_register_protocol(proto_frame, NULL);
700         prefs_register_bool_preference(frame_module, "show_file_off",
701             "Show File Offset", "Show offset of frame in capture file", &show_file_off);
702         prefs_register_bool_preference(frame_module, "force_docsis_encap",
703             "Treat all frames as DOCSIS frames", "Treat all frames as DOCSIS Frames", &force_docsis_encap);
704         prefs_register_bool_preference(frame_module, "generate_md5_hash",
705             "Generate an MD5 hash of each frame",
706             "Whether or not MD5 hashes should be generated for each frame, useful for finding duplicate frames.",
707             &generate_md5_hash);
708         prefs_register_bool_preference(frame_module, "generate_epoch_time",
709             "Generate an epoch time entry for each frame",
710             "Whether or not an Epoch time entry should be generated for each frame.",
711             &generate_epoch_time);
712         prefs_register_bool_preference(frame_module, "generate_bits_field",
713             "Show the number of bits in the frame",
714             "Whether or not the number of bits in the frame should be shown.",
715             &generate_bits_field);
716
717         frame_tap=register_tap("frame");
718 }
719
720 void
721 proto_reg_handoff_frame(void)
722 {
723         data_handle = find_dissector("data");
724         docsis_handle = find_dissector("docsis");
725 }