More Sysdig / system event support.
[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  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 2000 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include "config.h"
25
26 #ifdef _MSC_VER
27 #include <windows.h>
28 #endif
29
30 #include <epan/packet.h>
31 #include <epan/capture_dissectors.h>
32 #include <epan/epan.h>
33 #include <epan/exceptions.h>
34 #include <epan/show_exception.h>
35 #include <epan/timestamp.h>
36 #include <epan/prefs.h>
37 #include <epan/to_str.h>
38 #include <wiretap/wtap.h>
39 #include <epan/tap.h>
40 #include <epan/expert.h>
41 #include <wsutil/md5.h>
42 #include <wsutil/str_util.h>
43
44 #include "packet-frame.h"
45 #include "log.h"
46
47 #include <epan/color_filters.h>
48
49 void proto_register_frame(void);
50 void proto_reg_handoff_frame(void);
51
52 static int proto_frame = -1;
53 static int proto_pkt_comment = -1;
54 static int proto_syscall = -1;
55
56 static int hf_frame_arrival_time = -1;
57 static int hf_frame_shift_offset = -1;
58 static int hf_frame_arrival_time_epoch = -1;
59 static int hf_frame_time_delta = -1;
60 static int hf_frame_time_delta_displayed = -1;
61 static int hf_frame_time_relative = -1;
62 static int hf_frame_time_reference = -1;
63 static int hf_frame_number = -1;
64 static int hf_frame_len = -1;
65 static int hf_frame_capture_len = -1;
66 static int hf_frame_p2p_dir = -1;
67 static int hf_frame_file_off = -1;
68 static int hf_frame_md5_hash = -1;
69 static int hf_frame_marked = -1;
70 static int hf_frame_ignored = -1;
71 static int hf_link_number = -1;
72 static int hf_frame_protocols = -1;
73 static int hf_frame_color_filter_name = -1;
74 static int hf_frame_color_filter_text = -1;
75 static int hf_frame_interface_id = -1;
76 static int hf_frame_pack_flags = -1;
77 static int hf_frame_pack_direction = -1;
78 static int hf_frame_pack_reception_type = -1;
79 static int hf_frame_pack_fcs_length = -1;
80 static int hf_frame_pack_reserved = -1;
81 static int hf_frame_pack_crc_error = -1;
82 static int hf_frame_pack_wrong_packet_too_long_error = -1;
83 static int hf_frame_pack_wrong_packet_too_short_error = -1;
84 static int hf_frame_pack_wrong_inter_frame_gap_error = -1;
85 static int hf_frame_pack_unaligned_frame_error = -1;
86 static int hf_frame_pack_start_frame_delimiter_error = -1;
87 static int hf_frame_pack_preamble_error = -1;
88 static int hf_frame_pack_symbol_error = -1;
89 static int hf_frame_wtap_encap = -1;
90 static int hf_comments_text = -1;
91
92 static gint ett_frame = -1;
93 static gint ett_flags = -1;
94 static gint ett_comments = -1;
95
96 static expert_field ei_comments_text = EI_INIT;
97 static expert_field ei_arrive_time_out_of_range = EI_INIT;
98 static expert_field ei_incomplete = EI_INIT;
99
100 static int frame_tap = -1;
101
102 static dissector_handle_t docsis_handle;
103 static dissector_handle_t sysdig_handle;
104
105 /* Preferences */
106 static gboolean show_file_off       = FALSE;
107 static gboolean force_docsis_encap  = FALSE;
108 static gboolean generate_md5_hash   = FALSE;
109 static gboolean generate_epoch_time = TRUE;
110 static gboolean generate_bits_field = TRUE;
111 static gboolean disable_packet_size_limited_in_summary = FALSE;
112
113 static const value_string p2p_dirs[] = {
114         { P2P_DIR_UNKNOWN, "Unknown" },
115         { P2P_DIR_SENT,    "Sent" },
116         { P2P_DIR_RECV,    "Received" },
117         { 0, NULL }
118 };
119
120 #define PACKET_WORD_DIRECTION_MASK                        0x00000003
121 #define PACKET_WORD_RECEPTION_TYPE_MASK                   0x0000001C
122 #define PACKET_WORD_FCS_LENGTH_MASK                       0x000001E0
123 #define PACKET_WORD_RESERVED_MASK                         0x0000FE00
124 #define PACKET_WORD_CRC_ERR_MASK                          0x01000000
125 #define PACKET_WORD_PACKET_TOO_LONG_ERR_MASK              0x02000000
126 #define PACKET_WORD_PACKET_TOO_SHORT_ERR_MASK             0x04000000
127 #define PACKET_WORD_WRONG_INTER_FRAME_GAP_ERR_MASK        0x08000000
128 #define PACKET_WORD_UNALIGNED_FRAME_ERR_MASK              0x10000000
129 #define PACKET_WORD_START_FRAME_DELIMITER_ERR_MASK        0x20000000
130 #define PACKET_WORD_PREAMBLE_ERR_MASK                     0x40000000
131 #define PACKET_WORD_SYMBOL_ERR_MASK                       0x80000000
132
133 static const value_string packet_word_directions[] = {
134         { 0x00, "Not available" },
135         { 0x01, "Inbound" },
136         { 0x02, "Outbound" },
137         { 0x03, "Undefined" },
138         { 0, NULL }
139 };
140
141 static const value_string packet_word_reception_types[] = {
142         { 0x00, "Not specified" },
143         { 0x01, "Unicast" },
144         { 0x02, "Multicast" },
145         { 0x03, "Broadcast" },
146         { 0x04, "Promiscuous" },
147         { 0x05, "Undefined" },
148         { 0x06, "Undefined" },
149         { 0x07, "Undefined" },
150         { 0, NULL }
151 };
152
153 static dissector_table_t wtap_encap_dissector_table;
154 static dissector_table_t wtap_fts_rec_dissector_table;
155
156 /*
157  * Routine used to register frame end routine.  The routine should only
158  * be registered when the dissector is used in the frame, not in the
159  * proto_register_XXX function.
160  */
161 void
162 register_frame_end_routine(packet_info *pinfo, void (*func)(void))
163 {
164         pinfo->frame_end_routines = g_slist_append(pinfo->frame_end_routines, (gpointer)func);
165 }
166
167 typedef void (*void_func_t)(void);
168
169 static void
170 call_frame_end_routine(gpointer routine, gpointer dummy _U_)
171 {
172         void_func_t func = (void_func_t)routine;
173         (*func)();
174 }
175
176 static int
177 dissect_frame(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, void* data)
178 {
179         proto_item  *volatile ti = NULL, *comment_item;
180         guint        cap_len = 0, frame_len = 0;
181         proto_tree  *volatile tree;
182         proto_tree  *comments_tree;
183         proto_tree  *volatile fh_tree = NULL;
184         proto_item  *item;
185         const gchar *cap_plurality, *frame_plurality;
186         frame_data_t *fr_data = (frame_data_t*)data;
187         const color_filter_t *color_filter;
188
189         tree=parent_tree;
190
191         DISSECTOR_ASSERT(fr_data);
192
193         switch (pinfo->phdr->rec_type) {
194
195         case REC_TYPE_PACKET:
196                 pinfo->current_proto = "Frame";
197                 if (pinfo->pseudo_header != NULL) {
198                         switch (pinfo->pkt_encap) {
199
200                         case WTAP_ENCAP_WFLEET_HDLC:
201                         case WTAP_ENCAP_CHDLC_WITH_PHDR:
202                         case WTAP_ENCAP_PPP_WITH_PHDR:
203                         case WTAP_ENCAP_SDLC:
204                         case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
205                                 pinfo->p2p_dir = pinfo->pseudo_header->p2p.sent ?
206                                     P2P_DIR_SENT : P2P_DIR_RECV;
207                                 break;
208
209                         case WTAP_ENCAP_BLUETOOTH_HCI:
210                                 pinfo->p2p_dir = pinfo->pseudo_header->bthci.sent;
211                                 break;
212
213                         case WTAP_ENCAP_LAPB:
214                         case WTAP_ENCAP_FRELAY_WITH_PHDR:
215                                 pinfo->p2p_dir =
216                                     (pinfo->pseudo_header->x25.flags & FROM_DCE) ?
217                                     P2P_DIR_RECV : P2P_DIR_SENT;
218                                 break;
219
220                         case WTAP_ENCAP_ISDN:
221                         case WTAP_ENCAP_V5_EF:
222                         case WTAP_ENCAP_DPNSS:
223                         case WTAP_ENCAP_BACNET_MS_TP_WITH_PHDR:
224                                 pinfo->p2p_dir = pinfo->pseudo_header->isdn.uton ?
225                                     P2P_DIR_SENT : P2P_DIR_RECV;
226                                 break;
227
228                         case WTAP_ENCAP_LINUX_LAPD:
229                                 pinfo->p2p_dir = (pinfo->pseudo_header->lapd.pkttype == 3 ||
230                                         pinfo->pseudo_header->lapd.pkttype == 4) ?
231                                         P2P_DIR_SENT : P2P_DIR_RECV;
232                                 break;
233
234                         case WTAP_ENCAP_MTP2_WITH_PHDR:
235                                 pinfo->p2p_dir = pinfo->pseudo_header->mtp2.sent ?
236                                     P2P_DIR_SENT : P2P_DIR_RECV;
237                                 pinfo->link_number  = pinfo->pseudo_header->mtp2.link_number;
238                                 break;
239
240                         case WTAP_ENCAP_GSM_UM:
241                                 pinfo->p2p_dir = pinfo->pseudo_header->gsm_um.uplink ?
242                                     P2P_DIR_SENT : P2P_DIR_RECV;
243                                 break;
244                         }
245                 }
246                 break;
247
248         case REC_TYPE_FT_SPECIFIC_EVENT:
249                 pinfo->current_proto = "Event";
250                 break;
251
252         case REC_TYPE_FT_SPECIFIC_REPORT:
253                 pinfo->current_proto = "Report";
254                 break;
255
256         case REC_TYPE_SYSCALL:
257                 pinfo->current_proto = "System Call";
258                 break;
259
260         default:
261                 g_assert_not_reached();
262                 break;
263         }
264
265         if (fr_data->pkt_comment) {
266                 item = proto_tree_add_item(tree, proto_pkt_comment, tvb, 0, 0, ENC_NA);
267                 comments_tree = proto_item_add_subtree(item, ett_comments);
268                 comment_item = proto_tree_add_string_format(comments_tree, hf_comments_text, tvb, 0, 0,
269                                                                            fr_data->pkt_comment, "%s",
270                                                                            fr_data->pkt_comment);
271                 expert_add_info_format(pinfo, comment_item, &ei_comments_text,
272                                                                "%s",  fr_data->pkt_comment);
273
274
275         }
276
277         /* if FRAME is not referenced from any filters we don't need to worry about
278            generating any tree items.  */
279         if (!proto_field_is_referenced(tree, proto_frame)) {
280                 tree=NULL;
281                 if (pinfo->presence_flags & PINFO_HAS_TS) {
282                         if (pinfo->abs_ts.nsecs < 0 || pinfo->abs_ts.nsecs >= 1000000000)
283                                 expert_add_info(pinfo, NULL, &ei_arrive_time_out_of_range);
284                 }
285         } else {
286                 /* Put in frame header information. */
287                 cap_len = tvb_captured_length(tvb);
288                 frame_len = tvb_reported_length(tvb);
289
290                 cap_plurality = plurality(cap_len, "", "s");
291                 frame_plurality = plurality(frame_len, "", "s");
292
293                 switch (pinfo->phdr->rec_type) {
294                 case REC_TYPE_PACKET:
295                 case REC_TYPE_FT_SPECIFIC_EVENT:
296                 case REC_TYPE_FT_SPECIFIC_REPORT:
297                         ti = proto_tree_add_protocol_format(tree, proto_frame, tvb, 0, tvb_captured_length(tvb),
298                         "Frame %u: %u byte%s on wire",
299                         pinfo->num, frame_len, frame_plurality);
300                         if (generate_bits_field)
301                                 proto_item_append_text(ti, " (%u bits)", frame_len * 8);
302                         proto_item_append_text(ti, ", %u byte%s captured",
303                         cap_len, cap_plurality);
304                         if (generate_bits_field) {
305                                 proto_item_append_text(ti, " (%u bits)",
306                                 cap_len * 8);
307                         }
308                         break;
309
310                 case REC_TYPE_SYSCALL:
311                         /*
312                          * This gives us a top-of-tree "syscall" protocol
313                          * with "frame" fields underneath. Should we create
314                          * corresponding syscall.time, .time_epoch, etc
315                          * fields and use them instead or would frame.*
316                          * be preferred?
317                          */
318                         ti = proto_tree_add_protocol_format(tree, proto_syscall, tvb, 0, tvb_captured_length(tvb),
319                         "System Call %u: %u byte%s",
320                         pinfo->num, frame_len, frame_plurality);
321                         break;
322                 }
323
324                 if (pinfo->phdr->presence_flags & WTAP_HAS_INTERFACE_ID) {
325                         proto_item_append_text(ti, " on interface %u",
326                             pinfo->phdr->interface_id);
327                 }
328                 if (pinfo->phdr->presence_flags & WTAP_HAS_PACK_FLAGS) {
329                         if (pinfo->phdr->pack_flags & 0x00000001) {
330                                 proto_item_append_text(ti, " (inbound)");
331                                 pinfo->p2p_dir = P2P_DIR_RECV;
332                         }
333                         if (pinfo->phdr->pack_flags & 0x00000002) {
334                                 proto_item_append_text(ti, " (outbound)");
335                                 pinfo->p2p_dir = P2P_DIR_SENT;
336                         }
337                 }
338
339                 fh_tree = proto_item_add_subtree(ti, ett_frame);
340
341                 if (pinfo->phdr->presence_flags & WTAP_HAS_INTERFACE_ID && proto_field_is_referenced(tree, hf_frame_interface_id)) {
342                         const char *interface_name = epan_get_interface_name(pinfo->epan, pinfo->phdr->interface_id);
343
344                         if (interface_name)
345                                 proto_tree_add_uint_format_value(fh_tree, hf_frame_interface_id, tvb, 0, 0, pinfo->phdr->interface_id, "%u (%s)", pinfo->phdr->interface_id, interface_name);
346                         else
347                                 proto_tree_add_uint(fh_tree, hf_frame_interface_id, tvb, 0, 0, pinfo->phdr->interface_id);
348                 }
349
350                 if (pinfo->phdr->presence_flags & WTAP_HAS_PACK_FLAGS) {
351                         proto_tree *flags_tree;
352                         proto_item *flags_item;
353
354                         flags_item = proto_tree_add_uint(fh_tree, hf_frame_pack_flags, tvb, 0, 0, pinfo->phdr->pack_flags);
355                         flags_tree = proto_item_add_subtree(flags_item, ett_flags);
356                         proto_tree_add_uint(flags_tree, hf_frame_pack_direction, tvb, 0, 0, pinfo->phdr->pack_flags);
357                         proto_tree_add_uint(flags_tree, hf_frame_pack_reception_type, tvb, 0, 0, pinfo->phdr->pack_flags);
358                         proto_tree_add_uint(flags_tree, hf_frame_pack_fcs_length, tvb, 0, 0, pinfo->phdr->pack_flags);
359                         proto_tree_add_uint(flags_tree, hf_frame_pack_reserved, tvb, 0, 0, pinfo->phdr->pack_flags);
360                         proto_tree_add_boolean(flags_tree, hf_frame_pack_crc_error, tvb, 0, 0, pinfo->phdr->pack_flags);
361                         proto_tree_add_boolean(flags_tree, hf_frame_pack_wrong_packet_too_long_error, tvb, 0, 0, pinfo->phdr->pack_flags);
362                         proto_tree_add_boolean(flags_tree, hf_frame_pack_wrong_packet_too_short_error, tvb, 0, 0, pinfo->phdr->pack_flags);
363                         proto_tree_add_boolean(flags_tree, hf_frame_pack_wrong_inter_frame_gap_error, tvb, 0, 0, pinfo->phdr->pack_flags);
364                         proto_tree_add_boolean(flags_tree, hf_frame_pack_unaligned_frame_error, tvb, 0, 0, pinfo->phdr->pack_flags);
365                         proto_tree_add_boolean(flags_tree, hf_frame_pack_start_frame_delimiter_error, tvb, 0, 0, pinfo->phdr->pack_flags);
366                         proto_tree_add_boolean(flags_tree, hf_frame_pack_preamble_error, tvb, 0, 0, pinfo->phdr->pack_flags);
367                         proto_tree_add_boolean(flags_tree, hf_frame_pack_symbol_error, tvb, 0, 0, pinfo->phdr->pack_flags);
368                 }
369
370                 if (pinfo->phdr->rec_type == REC_TYPE_PACKET)
371                         proto_tree_add_int(fh_tree, hf_frame_wtap_encap, tvb, 0, 0, pinfo->pkt_encap);
372
373                 if (pinfo->presence_flags & PINFO_HAS_TS) {
374                         proto_tree_add_time(fh_tree, hf_frame_arrival_time, tvb,
375                                             0, 0, &(pinfo->abs_ts));
376                         if (pinfo->abs_ts.nsecs < 0 || pinfo->abs_ts.nsecs >= 1000000000) {
377                                 expert_add_info_format(pinfo, ti, &ei_arrive_time_out_of_range,
378                                                                   "Arrival Time: Fractional second %09ld is invalid,"
379                                                                   " the valid range is 0-1000000000",
380                                                                   (long) pinfo->abs_ts.nsecs);
381                         }
382                         item = proto_tree_add_time(fh_tree, hf_frame_shift_offset, tvb,
383                                             0, 0, &(pinfo->fd->shift_offset));
384                         PROTO_ITEM_SET_GENERATED(item);
385
386                         if (generate_epoch_time) {
387                                 proto_tree_add_time(fh_tree, hf_frame_arrival_time_epoch, tvb,
388                                                     0, 0, &(pinfo->abs_ts));
389                         }
390
391                         if (proto_field_is_referenced(tree, hf_frame_time_delta)) {
392                                 nstime_t     del_cap_ts;
393
394                                 frame_delta_abs_time(pinfo->epan, pinfo->fd, pinfo->num - 1, &del_cap_ts);
395
396                                 item = proto_tree_add_time(fh_tree, hf_frame_time_delta, tvb,
397                                                            0, 0, &(del_cap_ts));
398                                 PROTO_ITEM_SET_GENERATED(item);
399                         }
400
401                         if (proto_field_is_referenced(tree, hf_frame_time_delta_displayed)) {
402                                 nstime_t del_dis_ts;
403
404                                 frame_delta_abs_time(pinfo->epan, pinfo->fd, pinfo->fd->prev_dis_num, &del_dis_ts);
405
406                                 item = proto_tree_add_time(fh_tree, hf_frame_time_delta_displayed, tvb,
407                                                            0, 0, &(del_dis_ts));
408                                 PROTO_ITEM_SET_GENERATED(item);
409                         }
410
411                         item = proto_tree_add_time(fh_tree, hf_frame_time_relative, tvb,
412                                                    0, 0, &(pinfo->rel_ts));
413                         PROTO_ITEM_SET_GENERATED(item);
414
415                         if (pinfo->fd->flags.ref_time) {
416                                 ti = proto_tree_add_item(fh_tree, hf_frame_time_reference, tvb, 0, 0, ENC_NA);
417                                 PROTO_ITEM_SET_GENERATED(ti);
418                         }
419                 }
420
421                 proto_tree_add_uint(fh_tree, hf_frame_number, tvb,
422                                     0, 0, pinfo->num);
423
424                 proto_tree_add_uint_format(fh_tree, hf_frame_len, tvb,
425                                            0, 0, frame_len, "Frame Length: %u byte%s (%u bits)",
426                                            frame_len, frame_plurality, frame_len * 8);
427
428                 proto_tree_add_uint_format(fh_tree, hf_frame_capture_len, tvb,
429                                            0, 0, cap_len, "Capture Length: %u byte%s (%u bits)",
430                                            cap_len, cap_plurality, cap_len * 8);
431
432                 if (generate_md5_hash) {
433                         const guint8 *cp;
434                         md5_state_t   md_ctx;
435                         md5_byte_t    digest[16];
436                         const gchar  *digest_string;
437
438                         cp = tvb_get_ptr(tvb, 0, cap_len);
439
440                         md5_init(&md_ctx);
441                         md5_append(&md_ctx, cp, cap_len);
442                         md5_finish(&md_ctx, digest);
443
444                         digest_string = bytestring_to_str(wmem_packet_scope(), digest, 16, '\0');
445                         ti = proto_tree_add_string(fh_tree, hf_frame_md5_hash, tvb, 0, 0, digest_string);
446                         PROTO_ITEM_SET_GENERATED(ti);
447                 }
448
449                 ti = proto_tree_add_boolean(fh_tree, hf_frame_marked, tvb, 0, 0,pinfo->fd->flags.marked);
450                 PROTO_ITEM_SET_GENERATED(ti);
451
452                 ti = proto_tree_add_boolean(fh_tree, hf_frame_ignored, tvb, 0, 0,pinfo->fd->flags.ignored);
453                 PROTO_ITEM_SET_GENERATED(ti);
454
455                 /* Check for existences of P2P pseudo header */
456                 if (pinfo->p2p_dir != P2P_DIR_UNKNOWN) {
457                         proto_tree_add_int(fh_tree, hf_frame_p2p_dir, tvb,
458                                            0, 0, pinfo->p2p_dir);
459                 }
460
461                 /* Check for existences of MTP2 link number */
462                 if ((pinfo->pseudo_header != NULL ) && (pinfo->pkt_encap == WTAP_ENCAP_MTP2_WITH_PHDR)) {
463                         proto_tree_add_uint(fh_tree, hf_link_number, tvb,
464                                             0, 0, pinfo->link_number);
465                 }
466
467                 if (show_file_off) {
468                         proto_tree_add_int64_format_value(fh_tree, hf_frame_file_off, tvb,
469                                                     0, 0, pinfo->fd->file_off,
470                                                     "%" G_GINT64_MODIFIER "d (0x%" G_GINT64_MODIFIER "x)",
471                                                     pinfo->fd->file_off, pinfo->fd->file_off);
472                 }
473         }
474
475         if (pinfo->fd->flags.ignored) {
476                 /* Ignored package, stop handling here */
477                 col_set_str(pinfo->cinfo, COL_INFO, "<Ignored>");
478                 proto_tree_add_boolean_format(tree, hf_frame_ignored, tvb, 0, 0, TRUE, "This frame is marked as ignored");
479                 return tvb_captured_length(tvb);
480         }
481
482         /* Portable Exception Handling to trap Wireshark specific exceptions like BoundsError exceptions */
483         TRY {
484 #ifdef _MSC_VER
485                 /* Win32: Visual-C Structured Exception Handling (SEH) to trap hardware exceptions
486                    like memory access violations.
487                    (a running debugger will be called before the except part below) */
488                 /* Note: A Windows "exceptional exception" may leave the kazlib's (Portable Exception Handling)
489                    stack in an inconsistent state thus causing a crash at some point in the
490                    handling of the exception.
491                    See: https://www.wireshark.org/lists/wireshark-dev/200704/msg00243.html
492                 */
493                 __try {
494 #endif
495                         switch (pinfo->phdr->rec_type) {
496
497                         case REC_TYPE_PACKET:
498                                 if ((force_docsis_encap) && (docsis_handle)) {
499                                         call_dissector_with_data(docsis_handle,
500                                             tvb, pinfo, parent_tree,
501                                             (void *)pinfo->pseudo_header);
502                                 } else {
503                                         if (!dissector_try_uint_new(wtap_encap_dissector_table,
504                                             pinfo->pkt_encap, tvb, pinfo,
505                                             parent_tree, TRUE,
506                                             (void *)pinfo->pseudo_header)) {
507                                                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "UNKNOWN");
508                                                 col_add_fstr(pinfo->cinfo, COL_INFO, "WTAP_ENCAP = %d",
509                                                              pinfo->pkt_encap);
510                                                 call_data_dissector(tvb, pinfo, parent_tree);
511                                         }
512                                 }
513                                 break;
514
515                         case REC_TYPE_FT_SPECIFIC_EVENT:
516                         case REC_TYPE_FT_SPECIFIC_REPORT:
517                                 {
518                                         int file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_UNKNOWN;
519
520                                         file_type_subtype = fr_data->file_type_subtype;
521
522                                         if (!dissector_try_uint(wtap_fts_rec_dissector_table, file_type_subtype,
523                                             tvb, pinfo, parent_tree)) {
524                                                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "UNKNOWN");
525                                                 col_add_fstr(pinfo->cinfo, COL_INFO, "WTAP_ENCAP = %d",
526                                                              file_type_subtype);
527                                                 call_data_dissector(tvb, pinfo, parent_tree);
528                                         }
529                                 }
530                                 break;
531
532                         case REC_TYPE_SYSCALL:
533                                 /* Sysdig is the only type we currently handle. */
534                                 if (sysdig_handle) {
535                                         call_dissector_with_data(sysdig_handle,
536                                             tvb, pinfo, parent_tree,
537                                             (void *)pinfo->pseudo_header);
538                                 }
539                                 break;
540                         }
541 #ifdef _MSC_VER
542                 } __except(EXCEPTION_EXECUTE_HANDLER /* handle all exceptions */) {
543                         switch (GetExceptionCode()) {
544                         case(STATUS_ACCESS_VIOLATION):
545                                 show_exception(tvb, pinfo, parent_tree, DissectorError,
546                                                "STATUS_ACCESS_VIOLATION: dissector accessed an invalid memory address");
547                                 break;
548                         case(STATUS_INTEGER_DIVIDE_BY_ZERO):
549                                 show_exception(tvb, pinfo, parent_tree, DissectorError,
550                                                "STATUS_INTEGER_DIVIDE_BY_ZERO: dissector tried an integer division by zero");
551                                 break;
552                         case(STATUS_STACK_OVERFLOW):
553                                 show_exception(tvb, pinfo, parent_tree, DissectorError,
554                                                "STATUS_STACK_OVERFLOW: dissector overflowed the stack (e.g. endless loop)");
555                                 /* XXX - this will have probably corrupted the stack,
556                                    which makes problems later in the exception code */
557                                 break;
558                                 /* XXX - add other hardware exception codes as required */
559                         default:
560                                 show_exception(tvb, pinfo, parent_tree, DissectorError,
561                                                g_strdup_printf("dissector caused an unknown exception: 0x%x", GetExceptionCode()));
562                         }
563                 }
564 #endif
565         }
566         CATCH_BOUNDS_AND_DISSECTOR_ERRORS {
567                 show_exception(tvb, pinfo, parent_tree, EXCEPT_CODE, GET_MESSAGE);
568         }
569         ENDTRY;
570
571         if (proto_field_is_referenced(tree, hf_frame_protocols)) {
572                 wmem_strbuf_t *val = wmem_strbuf_sized_new(wmem_packet_scope(), 128, 0);
573                 wmem_list_frame_t *frame;
574                 /* skip the first entry, it's always the "frame" protocol */
575                 frame = wmem_list_frame_next(wmem_list_head(pinfo->layers));
576                 if (frame) {
577                         wmem_strbuf_append(val, proto_get_protocol_filter_name(GPOINTER_TO_UINT(wmem_list_frame_data(frame))));
578                         frame = wmem_list_frame_next(frame);
579                 }
580                 while (frame) {
581                         wmem_strbuf_append_c(val, ':');
582                         wmem_strbuf_append(val, proto_get_protocol_filter_name(GPOINTER_TO_UINT(wmem_list_frame_data(frame))));
583                         frame = wmem_list_frame_next(frame);
584                 }
585                 ti = proto_tree_add_string(fh_tree, hf_frame_protocols, tvb, 0, 0, wmem_strbuf_get_str(val));
586                 PROTO_ITEM_SET_GENERATED(ti);
587         }
588
589         /*  Call postdissectors if we have any (while trying to avoid another
590          *  TRY/CATCH)
591          */
592         if (have_postdissector()) {
593                 TRY {
594 #ifdef _MSC_VER
595                         /* Win32: Visual-C Structured Exception Handling (SEH)
596                            to trap hardware exceptions like memory access violations */
597                         /* (a running debugger will be called before the except part below) */
598                         /* Note: A Windows "exceptional exception" may leave the kazlib's (Portable Exception Handling)
599                            stack in an inconsistent state thus causing a crash at some point in the
600                            handling of the exception.
601                            See: https://www.wireshark.org/lists/wireshark-dev/200704/msg00243.html
602                         */
603                         __try {
604 #endif
605                                 call_all_postdissectors(tvb, pinfo, parent_tree);
606 #ifdef _MSC_VER
607                         } __except(EXCEPTION_EXECUTE_HANDLER /* handle all exceptions */) {
608                                 switch (GetExceptionCode()) {
609                                 case(STATUS_ACCESS_VIOLATION):
610                                         show_exception(tvb, pinfo, parent_tree, DissectorError,
611                                                        "STATUS_ACCESS_VIOLATION: dissector accessed an invalid memory address");
612                                         break;
613                                 case(STATUS_INTEGER_DIVIDE_BY_ZERO):
614                                         show_exception(tvb, pinfo, parent_tree, DissectorError,
615                                                        "STATUS_INTEGER_DIVIDE_BY_ZERO: dissector tried an integer division by zero");
616                                         break;
617                                 case(STATUS_STACK_OVERFLOW):
618                                         show_exception(tvb, pinfo, parent_tree, DissectorError,
619                                                        "STATUS_STACK_OVERFLOW: dissector overflowed the stack (e.g. endless loop)");
620                                         /* XXX - this will have probably corrupted the stack,
621                                            which makes problems later in the exception code */
622                                         break;
623                                         /* XXX - add other hardware exception codes as required */
624                                 default:
625                                         show_exception(tvb, pinfo, parent_tree, DissectorError,
626                                                        g_strdup_printf("dissector caused an unknown exception: 0x%x", GetExceptionCode()));
627                                 }
628                         }
629 #endif
630                 }
631                 CATCH_BOUNDS_AND_DISSECTOR_ERRORS {
632                         show_exception(tvb, pinfo, parent_tree, EXCEPT_CODE, GET_MESSAGE);
633                 }
634                 ENDTRY;
635         }
636
637         /* Attempt to (re-)calculate color filters (if any). */
638         if (pinfo->fd->flags.need_colorize) {
639                 color_filter = color_filters_colorize_packet(fr_data->color_edt);
640                 pinfo->fd->color_filter = color_filter;
641                 pinfo->fd->flags.need_colorize = 0;
642         } else {
643                 color_filter = pinfo->fd->color_filter;
644         }
645         if (color_filter) {
646                 item = proto_tree_add_string(fh_tree, hf_frame_color_filter_name, tvb,
647                                              0, 0, color_filter->filter_name);
648                 PROTO_ITEM_SET_GENERATED(item);
649                 item = proto_tree_add_string(fh_tree, hf_frame_color_filter_text, tvb,
650                                              0, 0, color_filter->filter_text);
651                 PROTO_ITEM_SET_GENERATED(item);
652         }
653
654         tap_queue_packet(frame_tap, pinfo, NULL);
655
656
657         if (pinfo->frame_end_routines) {
658                 g_slist_foreach(pinfo->frame_end_routines, &call_frame_end_routine, NULL);
659                 g_slist_free(pinfo->frame_end_routines);
660                 pinfo->frame_end_routines = NULL;
661         }
662
663         if (prefs.enable_incomplete_dissectors_check && tree && tree->tree_data->visible) {
664                 gchar* decoded;
665                 guint length;
666                 guint i;
667                 guint byte;
668                 guint bit;
669
670                 length = tvb_captured_length(tvb);
671                 decoded = proto_find_undecoded_data(tree, length);
672
673                 for (i = 0; i < length; i++) {
674                         byte = i / 8;
675                         bit = i % 8;
676                         if (!(decoded[byte] & (1 << bit))) {
677                                 field_info* fi = proto_find_field_from_offset(tree, i, tvb);
678                                 if (fi && fi->hfinfo->id != proto_frame) {
679                                         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_WARNING,
680                                                 "Dissector %s incomplete in frame %u: undecoded byte number %u "
681                                                 "(0x%.4X+%u)",
682                                                 (fi ? fi->hfinfo->abbrev : "[unknown]"),
683                                                 pinfo->num, i, i - i % 16, i % 16);
684                                         proto_tree_add_expert_format(tree, pinfo, &ei_incomplete, tvb, i, 1, "Undecoded byte number: %u (0x%.4X+%u)", i, i - i % 16, i % 16);
685                                 }
686                         }
687                 }
688         }
689
690         return tvb_captured_length(tvb);
691 }
692
693 void
694 proto_register_frame(void)
695 {
696         static hf_register_info hf[] = {
697                 { &hf_frame_arrival_time,
698                   { "Arrival Time", "frame.time",
699                     FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0x0,
700                     "Absolute time when this frame was captured", HFILL }},
701
702                 { &hf_frame_shift_offset,
703                   { "Time shift for this packet", "frame.offset_shift",
704                     FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
705                     "Time shift applied to this packet", HFILL }},
706
707                 { &hf_frame_arrival_time_epoch,
708                   { "Epoch Time", "frame.time_epoch",
709                     FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
710                     "Epoch time when this frame was captured", HFILL }},
711
712                 { &hf_frame_time_delta,
713                   { "Time delta from previous captured frame", "frame.time_delta",
714                     FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
715                     NULL, HFILL }},
716
717                 { &hf_frame_time_delta_displayed,
718                   { "Time delta from previous displayed frame", "frame.time_delta_displayed",
719                     FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
720                     NULL, HFILL }},
721
722                 { &hf_frame_time_relative,
723                   { "Time since reference or first frame", "frame.time_relative",
724                     FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
725                     "Time relative to time reference or first frame", HFILL }},
726
727                 { &hf_frame_time_reference,
728                   { "This is a Time Reference frame", "frame.ref_time",
729                     FT_NONE, BASE_NONE, NULL, 0x0,
730                     "This frame is a Time Reference frame", HFILL }},
731
732                 { &hf_frame_number,
733                   { "Frame Number", "frame.number",
734                     FT_UINT32, BASE_DEC, NULL, 0x0,
735                     NULL, HFILL }},
736
737                 { &hf_frame_len,
738                   { "Frame length on the wire", "frame.len",
739                     FT_UINT32, BASE_DEC, NULL, 0x0,
740                     NULL, HFILL }},
741
742                 { &hf_frame_capture_len,
743                   { "Frame length stored into the capture file", "frame.cap_len",
744                     FT_UINT32, BASE_DEC, NULL, 0x0,
745                     NULL, HFILL }},
746
747                 { &hf_frame_md5_hash,
748                   { "Frame MD5 Hash", "frame.md5_hash",
749                     FT_STRING, BASE_NONE, NULL, 0x0,
750                     NULL, HFILL }},
751
752                 { &hf_frame_p2p_dir,
753                   { "Point-to-Point Direction", "frame.p2p_dir",
754                     FT_INT8, BASE_DEC, VALS(p2p_dirs), 0x0,
755                     NULL, HFILL }},
756
757                 { &hf_link_number,
758                   { "Link Number", "frame.link_nr",
759                     FT_UINT16, BASE_DEC, NULL, 0x0,
760                     NULL, HFILL }},
761
762                 { &hf_frame_file_off,
763                   { "File Offset", "frame.file_off",
764                     FT_INT64, BASE_DEC, NULL, 0x0,
765                     NULL, HFILL }},
766
767                 { &hf_frame_marked,
768                   { "Frame is marked", "frame.marked",
769                     FT_BOOLEAN, BASE_NONE, NULL, 0x0,
770                     "Frame is marked in the GUI", HFILL }},
771
772                 { &hf_frame_ignored,
773                   { "Frame is ignored", "frame.ignored",
774                     FT_BOOLEAN, BASE_NONE, NULL, 0x0,
775                     "Frame is ignored by the dissectors", HFILL }},
776
777                 { &hf_frame_protocols,
778                   { "Protocols in frame", "frame.protocols",
779                     FT_STRING, BASE_NONE, NULL, 0x0,
780                     "Protocols carried by this frame", HFILL }},
781
782                 { &hf_frame_color_filter_name,
783                   { "Coloring Rule Name", "frame.coloring_rule.name",
784                     FT_STRING, BASE_NONE, NULL, 0x0,
785                     "The frame matched the coloring rule with this name", HFILL }},
786
787                 { &hf_frame_color_filter_text,
788                   { "Coloring Rule String", "frame.coloring_rule.string",
789                     FT_STRING, BASE_NONE, NULL, 0x0,
790                     "The frame matched this coloring rule string", HFILL }},
791
792                 { &hf_frame_interface_id,
793                   { "Interface id", "frame.interface_id",
794                     FT_UINT32, BASE_DEC, NULL, 0x0,
795                     NULL, HFILL }},
796
797                 { &hf_frame_pack_flags,
798                   { "Packet flags", "frame.packet_flags",
799                     FT_UINT32, BASE_HEX, NULL, 0x0,
800                     NULL, HFILL }},
801
802                 { &hf_frame_pack_direction,
803                   { "Direction", "frame.packet_flags_direction",
804                     FT_UINT32, BASE_HEX, VALS(packet_word_directions), PACKET_WORD_DIRECTION_MASK,
805                     NULL, HFILL }},
806
807                 { &hf_frame_pack_reception_type,
808                   { "Reception type", "frame.packet_flags_reception_type",
809                     FT_UINT32, BASE_DEC, VALS(packet_word_reception_types), PACKET_WORD_RECEPTION_TYPE_MASK,
810                     NULL, HFILL }},
811
812                 { &hf_frame_pack_fcs_length,
813                   { "FCS length", "frame.packet_flags_fcs_length",
814                     FT_UINT32, BASE_DEC, NULL, PACKET_WORD_FCS_LENGTH_MASK,
815                     NULL, HFILL }},
816
817                 { &hf_frame_pack_reserved,
818                   { "Reserved", "frame.packet_flags_reserved",
819                     FT_UINT32, BASE_DEC, NULL, PACKET_WORD_RESERVED_MASK,
820                     NULL, HFILL }},
821
822                 { &hf_frame_pack_crc_error,
823                   { "CRC error", "frame.packet_flags_crc_error",
824                     FT_BOOLEAN, 32, TFS(&tfs_set_notset), PACKET_WORD_CRC_ERR_MASK,
825                     NULL, HFILL }},
826
827                 { &hf_frame_pack_wrong_packet_too_long_error,
828                   { "Packet too long error", "frame.packet_flags_packet_too_error",
829                     FT_BOOLEAN, 32, TFS(&tfs_set_notset), PACKET_WORD_PACKET_TOO_LONG_ERR_MASK,
830                     NULL, HFILL }},
831
832                 { &hf_frame_pack_wrong_packet_too_short_error,
833                   { "Packet too short error", "frame.packet_flags_packet_too_short_error",
834                     FT_BOOLEAN, 32, TFS(&tfs_set_notset), PACKET_WORD_PACKET_TOO_SHORT_ERR_MASK,
835                     NULL, HFILL }},
836
837                 { &hf_frame_pack_wrong_inter_frame_gap_error,
838                   { "Wrong interframe gap error", "frame.packet_flags_wrong_inter_frame_gap_error",
839                     FT_BOOLEAN, 32, TFS(&tfs_set_notset), PACKET_WORD_WRONG_INTER_FRAME_GAP_ERR_MASK,
840                     NULL, HFILL }},
841
842                 { &hf_frame_pack_unaligned_frame_error,
843                   { "Unaligned frame error", "frame.packet_flags_unaligned_frame_error",
844                     FT_BOOLEAN, 32, TFS(&tfs_set_notset), PACKET_WORD_UNALIGNED_FRAME_ERR_MASK,
845                     NULL, HFILL }},
846
847                 { &hf_frame_pack_start_frame_delimiter_error,
848                   { "Start frame delimiter error", "frame.packet_flags_start_frame_delimiter_error",
849                     FT_BOOLEAN, 32, TFS(&tfs_set_notset), PACKET_WORD_START_FRAME_DELIMITER_ERR_MASK,
850                     NULL, HFILL }},
851
852                 { &hf_frame_pack_preamble_error,
853                   { "Preamble error", "frame.packet_flags_preamble_error",
854                     FT_BOOLEAN, 32, TFS(&tfs_set_notset), PACKET_WORD_PREAMBLE_ERR_MASK,
855                     NULL, HFILL }},
856
857                 { &hf_frame_pack_symbol_error,
858                   { "Symbol error", "frame.packet_flags_symbol_error",
859                     FT_BOOLEAN, 32, TFS(&tfs_set_notset), PACKET_WORD_SYMBOL_ERR_MASK,
860                     NULL, HFILL }},
861
862                 { &hf_comments_text,
863                   { "Comment", "frame.comment",
864                     FT_STRING, BASE_NONE, NULL, 0x0,
865                     NULL, HFILL }},
866         };
867
868         static hf_register_info hf_encap =
869                 { &hf_frame_wtap_encap,
870                   { "Encapsulation type", "frame.encap_type",
871                     FT_INT16, BASE_DEC, NULL, 0x0,
872                     NULL, HFILL }};
873
874         static gint *ett[] = {
875                 &ett_frame,
876                 &ett_flags,
877                 &ett_comments
878         };
879
880         static ei_register_info ei[] = {
881                 { &ei_comments_text, { "frame.comment.expert", PI_COMMENTS_GROUP, PI_COMMENT, "Formatted comment", EXPFILL }},
882                 { &ei_arrive_time_out_of_range, { "frame.time_invalid", PI_SEQUENCE, PI_NOTE, "Arrival Time: Fractional second out of range (0-1000000000)", EXPFILL }},
883                 { &ei_incomplete, { "frame.incomplete", PI_UNDECODED, PI_NOTE, "Incomplete dissector", EXPFILL }}
884         };
885
886         module_t *frame_module;
887         expert_module_t* expert_frame;
888
889         if (hf_encap.hfinfo.strings == NULL) {
890                 int encap_count = wtap_get_num_encap_types();
891                 value_string *arr;
892                 int i;
893
894                 hf_encap.hfinfo.strings = arr = g_new(value_string, encap_count+1);
895
896                 for (i = 0; i < encap_count; i++) {
897                         arr[i].value = i;
898                         arr[i].strptr = wtap_encap_string(i);
899                 }
900                 arr[encap_count].value = 0;
901                 arr[encap_count].strptr = NULL;
902         }
903
904         proto_frame = proto_register_protocol("Frame", "Frame", "frame");
905         proto_pkt_comment = proto_register_protocol("Packet comments", "Pkt_Comment", "pkt_comment");
906         proto_syscall = proto_register_protocol("System Call", "Syscall", "syscall");
907
908         proto_register_field_array(proto_frame, hf, array_length(hf));
909         proto_register_field_array(proto_frame, &hf_encap, 1);
910         proto_register_subtree_array(ett, array_length(ett));
911         expert_frame = expert_register_protocol(proto_frame);
912         expert_register_field_array(expert_frame, ei, array_length(ei));
913         register_dissector("frame",dissect_frame,proto_frame);
914
915         wtap_encap_dissector_table = register_dissector_table("wtap_encap",
916             "Wiretap encapsulation type", proto_frame, FT_UINT32, BASE_DEC, DISSECTOR_TABLE_ALLOW_DUPLICATE);
917         wtap_fts_rec_dissector_table = register_dissector_table("wtap_fts_rec",
918             "Wiretap file type for file-type-specific records", proto_frame, FT_UINT32, BASE_DEC, DISSECTOR_TABLE_ALLOW_DUPLICATE);
919         register_capture_dissector_table("wtap_encap", "Wiretap encapsulation type");
920
921         /* You can't disable dissection of "Frame", as that would be
922            tantamount to not doing any dissection whatsoever. */
923         proto_set_cant_toggle(proto_frame);
924
925         /* Our preferences */
926         frame_module = prefs_register_protocol(proto_frame, NULL);
927         prefs_register_bool_preference(frame_module, "show_file_off",
928             "Show File Offset", "Show offset of frame in capture file", &show_file_off);
929         prefs_register_bool_preference(frame_module, "force_docsis_encap",
930             "Treat all frames as DOCSIS frames", "Treat all frames as DOCSIS Frames", &force_docsis_encap);
931         prefs_register_bool_preference(frame_module, "generate_md5_hash",
932             "Generate an MD5 hash of each frame",
933             "Whether or not MD5 hashes should be generated for each frame, useful for finding duplicate frames.",
934             &generate_md5_hash);
935         prefs_register_bool_preference(frame_module, "generate_epoch_time",
936             "Generate an epoch time entry for each frame",
937             "Whether or not an Epoch time entry should be generated for each frame.",
938             &generate_epoch_time);
939         prefs_register_bool_preference(frame_module, "generate_bits_field",
940             "Show the number of bits in the frame",
941             "Whether or not the number of bits in the frame should be shown.",
942             &generate_bits_field);
943         prefs_register_bool_preference(frame_module, "disable_packet_size_limited_in_summary",
944             "Disable 'packet size limited during capture' message in summary",
945             "Whether or not 'packet size limited during capture' message in shown in Info column.",
946             &disable_packet_size_limited_in_summary);
947
948         frame_tap=register_tap("frame");
949 }
950
951 void
952 proto_reg_handoff_frame(void)
953 {
954         docsis_handle = find_dissector_add_dependency("docsis", proto_frame);
955         sysdig_handle = find_dissector_add_dependency("sysdig", proto_frame);
956 }
957
958 /*
959  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
960  *
961  * Local variables:
962  * c-basic-offset: 8
963  * tab-width: 8
964  * indent-tabs-mode: t
965  * End:
966  *
967  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
968  * :indentSize=8:tabSize=8:noTabs=false:
969  */