Coverage Report

Created: 2026-05-14 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wireshark/epan/dissectors/packet-eth.c
Line
Count
Source
1
/* packet-eth.c
2
 * Routines for ethernet packet disassembly
3
 *
4
 * Wireshark - Network traffic analyzer
5
 * By Gerald Combs <gerald@wireshark.org>
6
 * Copyright 1998 Gerald Combs
7
 *
8
 * SPDX-License-Identifier: GPL-2.0-or-later
9
 */
10
11
#include "config.h"
12
13
#include <epan/packet.h>
14
#include <epan/exceptions.h>
15
#include <epan/prefs.h>
16
#include <epan/etypes.h>
17
#include <epan/addr_resolv.h>
18
#include <epan/expert.h>
19
#include <epan/conversation_table.h>
20
#include <epan/conversation_filter.h>
21
#include <epan/capture_dissectors.h>
22
#include <epan/exported_pdu.h>
23
#include <epan/tfs.h>
24
#include <wsutil/pint.h>
25
#include "packet-eth.h"
26
#include "packet-gre.h"
27
#include "packet-ieee8023.h"
28
#include "packet-ipx.h"
29
#include "packet-isl.h"
30
#include "packet-llc.h"
31
#include "packet-sll.h"
32
#include "packet-juniper.h"
33
#include "packet-sflow.h"
34
#include "packet-l2tp.h"
35
#include "packet-vxlan.h"
36
#include "packet-nsh.h"
37
#include "packet-acdr.h"
38
#include "packet-mctp.h"
39
#include <epan/iana-info.h>
40
#include <epan/crc32-tvb.h>
41
#include <wiretap/erf_record.h>
42
43
void proto_register_eth(void);
44
void proto_reg_handoff_eth(void);
45
46
9.70k
#define PADDING_NONE        0
47
529
#define PADDING_ZEROS       1
48
5.19k
#define PADDING_ANY         2
49
50
static int eth_padding = PADDING_ZEROS;
51
static unsigned eth_trailer_length;
52
/* By default, try to autodetect FCS */
53
static int eth_fcs = -1;
54
static bool eth_check_fcs;
55
/* Interpret packets as FW1 monitor file packets if they look as if they are */
56
static bool eth_interpret_as_fw1_monitor;
57
/* When capturing on a Cisco FEX some frames start with an extra destination mac */
58
static bool eth_deduplicate_dmac;
59
/* Preference settings defining conditions for which the CCSDS dissector is called */
60
static bool ccsds_heuristic_length;
61
static bool ccsds_heuristic_version;
62
static bool ccsds_heuristic_header;
63
static bool ccsds_heuristic_bit;
64
65
/* protocols and header fields */
66
static int proto_eth;
67
static int hf_eth_dst;
68
static int hf_eth_dst_resolved;
69
static int hf_eth_dst_oui;
70
static int hf_eth_dst_oui_resolved;
71
static int hf_eth_src;
72
static int hf_eth_src_resolved;
73
static int hf_eth_src_oui;
74
static int hf_eth_src_oui_resolved;
75
static int hf_eth_len;
76
static int hf_eth_type;
77
static int hf_eth_invalid_lentype;
78
static int hf_eth_addr;
79
static int hf_eth_addr_resolved;
80
static int hf_eth_addr_oui;
81
static int hf_eth_addr_oui_resolved;
82
static int hf_eth_dst_lg;
83
static int hf_eth_dst_ig;
84
static int hf_eth_src_lg;
85
static int hf_eth_src_ig;
86
static int hf_eth_lg;
87
static int hf_eth_ig;
88
static int hf_eth_padding;
89
static int hf_eth_trailer;
90
static int hf_eth_fcs;
91
static int hf_eth_fcs_status;
92
static int hf_eth_stream;
93
94
static int ett_ieee8023;
95
static int ett_ether2;
96
static int ett_ether;
97
static int ett_addr;
98
99
static expert_field ei_eth_invalid_lentype;
100
static expert_field ei_eth_src_not_group;
101
static expert_field ei_eth_fcs_bad;
102
static expert_field ei_eth_len;
103
static expert_field ei_eth_padding_bad;
104
105
static dissector_handle_t fw1_handle;
106
static dissector_handle_t ethertype_handle;
107
static capture_dissector_handle_t isl_cap_handle;
108
static capture_dissector_handle_t ipx_cap_handle;
109
static capture_dissector_handle_t llc_cap_handle;
110
static heur_dissector_list_t heur_subdissector_list;
111
static heur_dissector_list_t eth_trailer_subdissector_list;
112
static dissector_handle_t eth_withoutfcs_handle;
113
static dissector_handle_t eth_maybefcs_handle;
114
static dissector_handle_t eth_header_only_handle;
115
116
117
static int eth_tap;
118
static uint32_t eth_stream_count;
119
120
static int exported_pdu_tap = -1;
121
122
27.3k
#define ETH_HEADER_SIZE    14
123
124
static const true_false_string ig_tfs = {
125
  "Group address (multicast/broadcast)",
126
  "Individual address (unicast)"
127
};
128
static const true_false_string lg_tfs = {
129
  "Locally administered address (this is NOT the factory default)",
130
  "Globally unique address (factory default)"
131
};
132
133
static const enum_val_t eth_padding_vals[] = {
134
  {"never", "Never", PADDING_NONE},
135
  {"zeros", "Zeros", PADDING_ZEROS},
136
  {"any",   "Any",   PADDING_ANY},
137
  {NULL, NULL, 0}
138
};
139
140
static const enum_val_t eth_fcs_vals[] = {
141
  {"heuristic", "According to heuristic", -1},
142
  {"never",     "Never",                   0},
143
  {"always",    "Always",                  4},
144
  {NULL, NULL, 0}
145
};
146
147
static const char* eth_conv_get_filter_type(conv_item_t* conv, conv_filter_type_e filter)
148
0
{
149
0
  if ((filter == CONV_FT_SRC_ADDRESS) && (conv->src_address.type == AT_ETHER))
150
0
    return "eth.src";
151
152
0
  if ((filter == CONV_FT_DST_ADDRESS) && (conv->dst_address.type == AT_ETHER))
153
0
    return "eth.dst";
154
155
0
  if ((filter == CONV_FT_ANY_ADDRESS) && (conv->src_address.type == AT_ETHER))
156
0
    return "eth.addr";
157
158
0
  return CONV_FILTER_INVALID;
159
0
}
160
161
static ct_dissector_info_t eth_ct_dissector_info = {&eth_conv_get_filter_type};
162
163
static tap_packet_status
164
eth_conversation_packet(void *pct, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vip, tap_flags_t flags)
165
0
{
166
0
  conv_hash_t *hash = (conv_hash_t*) pct;
167
0
  hash->flags = flags;
168
0
  const eth_hdr *ehdr=(const eth_hdr *)vip;
169
170
0
  add_conversation_table_data_with_conv_id(hash, &ehdr->src, &ehdr->dst, 0, 0, (conv_id_t)ehdr->stream, 1, pinfo->fd->pkt_len,
171
0
                                           &pinfo->rel_ts, &pinfo->abs_ts, &eth_ct_dissector_info, CONVERSATION_ETH);
172
173
0
  return TAP_PACKET_REDRAW;
174
0
}
175
176
static const char* eth_endpoint_get_filter_type(endpoint_item_t* endpoint, conv_filter_type_e filter)
177
0
{
178
0
  if ((filter == CONV_FT_ANY_ADDRESS) && (endpoint->myaddress.type == AT_ETHER))
179
0
    return "eth.addr";
180
181
0
  return CONV_FILTER_INVALID;
182
0
}
183
184
static et_dissector_info_t eth_endpoint_dissector_info = {&eth_endpoint_get_filter_type};
185
186
static tap_packet_status
187
eth_endpoint_packet(void *pit, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vip, tap_flags_t flags)
188
0
{
189
0
  conv_hash_t *hash = (conv_hash_t*) pit;
190
0
  hash->flags = flags;
191
0
  const eth_hdr *ehdr=(const eth_hdr *)vip;
192
193
  /* Take two "add" passes per packet, adding for each direction, ensures that all
194
     packets are counted properly (even if address is sending to itself)
195
     XXX - this could probably be done more efficiently inside endpoint_table */
196
0
  add_endpoint_table_data(hash, &ehdr->src, 0, true, 1, pinfo->fd->pkt_len, &eth_endpoint_dissector_info, ENDPOINT_NONE);
197
0
  add_endpoint_table_data(hash, &ehdr->dst, 0, false, 1, pinfo->fd->pkt_len, &eth_endpoint_dissector_info, ENDPOINT_NONE);
198
199
0
  return TAP_PACKET_REDRAW;
200
0
}
201
202
static bool
203
eth_filter_valid(packet_info *pinfo, void *user_data _U_)
204
0
{
205
0
    return (pinfo->dl_src.type == AT_ETHER);
206
0
}
207
208
static char*
209
eth_build_filter(packet_info *pinfo, void *user_data _U_)
210
0
{
211
0
    return ws_strdup_printf("eth.addr eq %s and eth.addr eq %s",
212
0
                address_to_str(pinfo->pool, &pinfo->dl_src),
213
0
                address_to_str(pinfo->pool, &pinfo->dl_dst));
214
0
}
215
216
217
/* These are the Netware-ish names for the different Ethernet frame types.
218
    EthernetII: The ethernet with a Type field instead of a length field
219
    Ethernet802.2: An 802.3 header followed by an 802.2 header
220
    Ethernet802.3: A raw 802.3 packet. IPX/SPX can be the only payload.
221
            There's no 802.2 hdr in this.
222
    EthernetSNAP: Basically 802.2, just with 802.2SNAP. For our purposes,
223
        there's no difference between 802.2 and 802.2SNAP, since we just
224
        pass it down to the LLC dissector. -- Gilbert
225
*/
226
0
#define ETHERNET_II     0
227
0
#define ETHERNET_802_2  1
228
0
#define ETHERNET_802_3  2
229
#define ETHERNET_SNAP   3
230
231
static bool
232
capture_eth(const unsigned char *pd, int offset, int len, capture_packet_info_t *cpinfo, const union wtap_pseudo_header *pseudo_header)
233
0
{
234
0
  uint16_t etype, length;
235
0
  int ethhdr_type;          /* the type of ethernet frame */
236
237
0
  if (!BYTES_ARE_IN_FRAME(offset, len, ETH_HEADER_SIZE))
238
0
    return false;
239
240
0
  etype = pntohu16(&pd[offset+12]);
241
242
0
  if (etype <= IEEE_802_3_MAX_LEN) {
243
    /* Oh, yuck.  Cisco ISL frames require special interpretation of the
244
       destination address field; fortunately, they can be recognized by
245
       checking the first 5 octets of the destination address, which are
246
       01-00-0C-00-00 or 0C-00-0C-00-00 for ISL frames. */
247
0
    if ((pd[offset] == 0x01 || pd[offset] == 0x0C) && pd[offset+1] == 0x00
248
0
        && pd[offset+2] == 0x0C && pd[offset+3] == 0x00
249
0
        && pd[offset+4] == 0x00) {
250
0
      return call_capture_dissector(isl_cap_handle, pd, offset, len, cpinfo, pseudo_header);
251
0
    }
252
0
  }
253
254
  /*
255
   * If the type/length field is <= the maximum 802.3 length,
256
   * and is not zero, this is an 802.3 frame, and it's a length
257
   * field; it might be an Novell "raw 802.3" frame, with no
258
   * 802.2 LLC header, or it might be a frame with an 802.2 LLC
259
   * header.
260
   *
261
   * If the type/length field is >= the minimum Ethernet II length,
262
   * this is an Ethernet II frame, and it's a type field.
263
   *
264
   * If the type/length field is > maximum 802.3 length and < minimum
265
   * Ethernet II length, then this is an invalid packet.
266
   *
267
   * If the type/length field is zero (ETHERTYPE_UNK), this is
268
   * a frame used internally by the Cisco MDS switch to contain
269
   * Fibre Channel ("Vegas").  We treat that as an Ethernet II
270
   * frame; the dissector for those frames registers itself with
271
   * an ethernet type of ETHERTYPE_UNK.
272
   */
273
0
  if (etype > IEEE_802_3_MAX_LEN && etype < ETHERNET_II_MIN_LEN)
274
0
    return false;
275
276
0
  if (etype <= IEEE_802_3_MAX_LEN && etype != ETHERTYPE_UNK) {
277
0
    length = etype;
278
279
    /* Is there an 802.2 layer? I can tell by looking at the first 2
280
       bytes after the 802.3 header. If they are 0xffff, then what
281
       follows the 802.3 header is an IPX payload, meaning no 802.2.
282
       (IPX/SPX is they only thing that can be contained inside a
283
       straight 802.3 packet). A non-0xffff value means that there's an
284
       802.2 layer inside the 802.3 layer */
285
0
    if (pd[offset+14] == 0xff && pd[offset+15] == 0xff) {
286
0
      ethhdr_type = ETHERNET_802_3;
287
0
    }
288
0
    else {
289
0
      ethhdr_type = ETHERNET_802_2;
290
0
    }
291
292
    /* Convert the LLC length from the 802.3 header to a total
293
       frame length, by adding in the size of any data that preceded
294
       the Ethernet header, and adding in the Ethernet header size,
295
       and set the payload and captured-payload lengths to the minima
296
       of the total length and the frame lengths. */
297
0
    length += offset + ETH_HEADER_SIZE;
298
0
    if (len > length)
299
0
      len = length;
300
0
  } else {
301
0
    ethhdr_type = ETHERNET_II;
302
0
  }
303
0
  offset += ETH_HEADER_SIZE;
304
305
0
  switch (ethhdr_type) {
306
0
    case ETHERNET_802_3:
307
0
      return call_capture_dissector(ipx_cap_handle, pd, offset, len, cpinfo, pseudo_header);
308
0
    case ETHERNET_802_2:
309
0
      return call_capture_dissector(llc_cap_handle, pd, offset, len, cpinfo, pseudo_header);
310
0
    case ETHERNET_II:
311
0
      return try_capture_dissector("ethertype", etype, pd, offset, len, cpinfo, pseudo_header);
312
0
  }
313
314
0
  return false;
315
0
}
316
317
static bool check_is_802_2(tvbuff_t *tvb, int fcs_len);
318
319
static void
320
dissect_address_data(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, bool check_group)
321
13.6k
{
322
13.6k
  static const mac_hf_list_t eth_dst = {
323
13.6k
    &hf_eth_dst,
324
13.6k
    &hf_eth_dst_resolved,
325
13.6k
    &hf_eth_dst_oui,
326
13.6k
    &hf_eth_dst_oui_resolved,
327
13.6k
    &hf_eth_dst_lg,
328
13.6k
    &hf_eth_dst_ig,
329
13.6k
  };
330
13.6k
  static const mac_hf_list_t eth_src = {
331
13.6k
    &hf_eth_src,
332
13.6k
    &hf_eth_src_resolved,
333
13.6k
    &hf_eth_src_oui,
334
13.6k
    &hf_eth_src_oui_resolved,
335
13.6k
    &hf_eth_src_lg,
336
13.6k
    &hf_eth_src_ig,
337
13.6k
  };
338
13.6k
  static const mac_hf_list_t eth_addr = {
339
13.6k
    &hf_eth_addr,
340
13.6k
    &hf_eth_addr_resolved,
341
13.6k
    &hf_eth_addr_oui,
342
13.6k
    &hf_eth_addr_oui_resolved,
343
13.6k
    &hf_eth_lg,
344
13.6k
    &hf_eth_ig,
345
13.6k
  };
346
13.6k
  proto_item *addr_item;
347
348
13.6k
  proto_tree_add_mac48_detail(&eth_dst, &eth_addr, ett_addr, tvb, tree, 0);
349
350
13.6k
  addr_item = proto_tree_add_mac48_detail(&eth_src, &eth_addr, ett_addr, tvb, tree, 6);
351
13.6k
  if (check_group) {
352
9.34k
    if (tvb_get_uint8(tvb, 6) & 0x01) {
353
4.02k
      expert_add_info(pinfo, addr_item, &ei_eth_src_not_group);
354
4.02k
    }
355
9.34k
  }
356
13.6k
}
357
358
static void
359
export_pdu(tvbuff_t *tvb, packet_info *pinfo)
360
13.6k
{
361
13.6k
  if (have_tap_listener(exported_pdu_tap)) {
362
0
    exp_pdu_data_t *exp_pdu_data = wmem_new0(pinfo->pool, exp_pdu_data_t);
363
364
0
    exp_pdu_data->tvb_captured_length = tvb_captured_length(tvb);
365
0
    exp_pdu_data->tvb_reported_length = tvb_reported_length(tvb);
366
0
    exp_pdu_data->pdu_tvb = tvb;
367
0
    tap_queue_packet(exported_pdu_tap, pinfo, exp_pdu_data);
368
0
  }
369
13.6k
}
370
371
static struct eth_analysis *
372
init_eth_conversation_data(packet_info *pinfo)
373
3.91k
{
374
3.91k
    struct eth_analysis *ethd;
375
376
    /* Initialize the eth protocol data structure to add to the ip conversation */
377
3.91k
    ethd=wmem_new0(wmem_file_scope(), struct eth_analysis);
378
379
3.91k
    ethd->initial_frame = pinfo->num;
380
3.91k
    ethd->stream = 0;
381
3.91k
    ethd->stream = eth_stream_count++;
382
383
3.91k
    return ethd;
384
3.91k
}
385
386
struct eth_analysis *
387
get_eth_conversation_data(conversation_t *conv, packet_info *pinfo)
388
13.6k
{
389
13.6k
  struct eth_analysis *ethd;
390
391
  /* Did the caller supply the conversation pointer? */
392
13.6k
  if( conv==NULL ) {
393
0
    return NULL;
394
0
  }
395
396
  /* Get the data for this conversation */
397
13.6k
  ethd=(struct eth_analysis *)conversation_get_proto_data(conv, proto_eth);
398
399
13.6k
  if (!ethd) {
400
3.91k
    ethd = init_eth_conversation_data(pinfo);
401
3.91k
    conversation_add_proto_data(conv, proto_eth, ethd);
402
3.91k
  }
403
404
13.6k
  if (!ethd) {
405
0
    return NULL;
406
0
  }
407
408
13.6k
  return ethd;
409
13.6k
}
410
411
static proto_tree *
412
dissect_eth_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree,
413
    int fcs_len, bool header_only)
414
13.7k
{
415
13.7k
  proto_item        *ti = NULL;
416
13.7k
  eth_hdr           *ehdr;
417
13.7k
  bool              is_802_2;
418
13.7k
  proto_tree        *fh_tree = NULL;
419
13.7k
  static eth_hdr    ehdrs[4];
420
13.7k
  static int        ehdr_num=0;
421
13.7k
  proto_tree        *tree;
422
13.7k
  ethertype_data_t  ethertype_data;
423
13.7k
  heur_dtbl_entry_t *hdtbl_entry = NULL;
424
13.7k
  struct eth_analysis *ethd=NULL;
425
  /* a facility for not duplicating long code */
426
13.7k
  bool              needs_dissector_with_data = false;
427
428
  /* Rotating buffer */
429
13.7k
  ehdr_num++;
430
13.7k
  if(ehdr_num>=4){
431
3.44k
     ehdr_num=0;
432
3.44k
  }
433
13.7k
  ehdr=&ehdrs[ehdr_num];
434
435
13.7k
  tree=parent_tree;
436
437
13.7k
  col_set_str(pinfo->cinfo, COL_PROTOCOL, "Ethernet");
438
439
13.7k
  set_address_tvb(&pinfo->dl_dst, AT_ETHER, 6, tvb, 0);
440
13.7k
  copy_address_shallow(&pinfo->dst, &pinfo->dl_dst);
441
13.7k
  copy_address_shallow(&ehdr->dst, &pinfo->dl_dst);
442
443
13.7k
  set_address_tvb(&pinfo->dl_src, AT_ETHER, 6, tvb, 6);
444
13.7k
  copy_address_shallow(&pinfo->src, &pinfo->dl_src);
445
13.7k
  copy_address_shallow(&ehdr->src, &pinfo->dl_src);
446
447
13.7k
  ehdr->type = tvb_get_ntohs(tvb, 12);
448
449
13.7k
  tap_queue_packet(eth_tap, pinfo, ehdr);
450
13.7k
  export_pdu(tvb, pinfo);
451
452
  /*
453
   * In case the packet is a non-Ethernet packet inside
454
   * Ethernet framing, allow heuristic dissectors to take
455
   * a first look before we assume that it's actually an
456
   * Ethernet packet.
457
   *
458
   * Tell the heuristic dissectors what FCS length was reported for
459
   * this packet - the non-Ethernet packet might or might not include
460
   * the FCS, or might have a different calculation. (Heuristic
461
   * dissectors also can't report a number of bytes consumed, so we
462
   * can't really handle the "maybefcs" case otherwise.)
463
   */
464
13.7k
  struct eth_phdr    phdr;
465
13.7k
  phdr.fcs_len = fcs_len;
466
13.7k
  if (dissector_try_heuristic(heur_subdissector_list, tvb, pinfo, parent_tree, &hdtbl_entry, &phdr))
467
32
    return fh_tree;
468
469
13.7k
  if (ehdr->type <= IEEE_802_3_MAX_LEN) {
470
    /* Oh, yuck.  Cisco ISL frames require special interpretation of the
471
       destination address field; fortunately, they can be recognized by
472
       checking the first 5 octets of the destination address, which are
473
       01-00-0C-00-00 for ISL frames. */
474
4.60k
    if ((tvb_get_uint8(tvb, 0) == 0x01 ||
475
4.42k
      tvb_get_uint8(tvb, 0) == 0x0C) &&
476
206
      tvb_get_uint8(tvb, 1) == 0x00 &&
477
158
      tvb_get_uint8(tvb, 2) == 0x0C &&
478
42
      tvb_get_uint8(tvb, 3) == 0x00 &&
479
41
      tvb_get_uint8(tvb, 4) == 0x00) {
480
41
      dissect_isl(tvb, pinfo, parent_tree, fcs_len);
481
41
      return fh_tree;
482
41
    }
483
4.60k
  }
484
485
  /*
486
   * If the type/length field is <= the maximum 802.3 length,
487
   * and is not zero, this is an 802.3 frame, and it's a length
488
   * field; it might be an Novell "raw 802.3" frame, with no
489
   * 802.2 LLC header, or it might be a frame with an 802.2 LLC
490
   * header.
491
   *
492
   * If the type/length field is >= the minimum Ethernet II length,
493
   * this is an Ethernet II frame, and it's a type field.
494
   *
495
   * If the type/length field is > maximum 802.3 length and < minimum
496
   * Ethernet II length, then this is an invalid packet.
497
   *
498
   * If the type/length field is zero (ETHERTYPE_UNK), this is
499
   * a frame used internally by the Cisco MDS switch to contain
500
   * Fibre Channel ("Vegas").  We treat that as an Ethernet II
501
   * frame; the dissector for those frames registers itself with
502
   * an ethernet type of ETHERTYPE_UNK.
503
   */
504
13.6k
  if (ehdr->type > IEEE_802_3_MAX_LEN && ehdr->type < ETHERNET_II_MIN_LEN) {
505
4
    tvbuff_t *next_tvb;
506
507
4
    col_add_fstr(pinfo->cinfo, COL_INFO,
508
4
        "Ethernet Unknown: Invalid length/type: 0x%04x (%d)",
509
4
        ehdr->type, ehdr->type);
510
4
    ti = proto_tree_add_protocol_format(tree, proto_eth, tvb, 0, ETH_HEADER_SIZE,
511
4
        "Ethernet Unknown, Src: %s, Dst: %s",
512
4
        address_with_resolution_to_str(pinfo->pool, &pinfo->src),
513
4
        address_with_resolution_to_str(pinfo->pool, &pinfo->dst));
514
4
    fh_tree = proto_item_add_subtree(ti, ett_ether);
515
516
4
    dissect_address_data(tvb, pinfo, fh_tree, false);
517
518
4
    ti = proto_tree_add_item(fh_tree, hf_eth_invalid_lentype, tvb, 12, 2, ENC_BIG_ENDIAN);
519
4
    expert_add_info_format(pinfo, ti, &ei_eth_invalid_lentype,
520
4
        "Invalid length/type: 0x%04x (%d)", ehdr->type, ehdr->type);
521
4
    next_tvb = tvb_new_subset_remaining(tvb, 14);
522
4
    call_data_dissector(next_tvb, pinfo, parent_tree);
523
4
    return fh_tree;
524
4
  }
525
526
13.6k
  if (ehdr->type <= IEEE_802_3_MAX_LEN && ehdr->type != ETHERTYPE_UNK) {
527
528
4.27k
    is_802_2 = check_is_802_2(tvb, fcs_len);
529
530
4.27k
    col_add_fstr(pinfo->cinfo, COL_INFO, "IEEE 802.3 Ethernet %s",
531
4.27k
        (is_802_2 ? "" : "Raw "));
532
4.27k
    if (tree) {
533
4.27k
      ti = proto_tree_add_protocol_format(tree, proto_eth, tvb, 0, ETH_HEADER_SIZE,
534
4.27k
        "IEEE 802.3 Ethernet %s", (is_802_2 ? "" : "Raw "));
535
536
4.27k
      fh_tree = proto_item_add_subtree(ti, ett_ieee8023);
537
4.27k
    }
538
539
    /* if IP is not referenced from any filters we don't need to worry about
540
       generating any tree items.  We must do this after we created the actual
541
       protocol above so that proto hier stat still works though.
542
    */
543
4.27k
    if(!proto_field_is_referenced(parent_tree, proto_eth)){
544
733
      tree=NULL;
545
733
      fh_tree=NULL;
546
733
    }
547
548
4.27k
    dissect_address_data(tvb, pinfo, fh_tree, false);
549
550
4.27k
    dissect_802_3(ehdr->type, is_802_2, tvb, ETH_HEADER_SIZE, pinfo,
551
4.27k
        parent_tree, fh_tree, hf_eth_len, hf_eth_trailer, &ei_eth_len, fcs_len);
552
9.42k
  } else {
553
9.42k
    if (eth_interpret_as_fw1_monitor) {
554
0
        const uint8_t *dst_addr = (const uint8_t*)pinfo->dst.data;
555
556
0
        if ((dst_addr[0] == 'i') || (dst_addr[0] == 'I') ||
557
0
            (dst_addr[0] == 'o') || (dst_addr[0] == 'O') ||
558
0
            (dst_addr[0] == 'e') || (dst_addr[0] == 'E')) {
559
0
            call_dissector(fw1_handle, tvb, pinfo, parent_tree);
560
0
            return fh_tree;
561
0
        }
562
0
    }
563
564
9.42k
    col_set_str(pinfo->cinfo, COL_INFO, "Ethernet II");
565
9.42k
    if (parent_tree) {
566
9.34k
        if (PTREE_DATA(parent_tree)->visible) {
567
8.41k
            ti = proto_tree_add_protocol_format(parent_tree, proto_eth, tvb, 0, ETH_HEADER_SIZE,
568
8.41k
                "Ethernet II, Src: %s, Dst: %s",
569
8.41k
                address_with_resolution_to_str(pinfo->pool, &pinfo->src),
570
8.41k
                address_with_resolution_to_str(pinfo->pool, &pinfo->dst));
571
8.41k
      }
572
926
      else {
573
926
            ti = proto_tree_add_item(parent_tree, proto_eth, tvb, 0, ETH_HEADER_SIZE, ENC_NA);
574
926
      }
575
9.34k
      fh_tree = proto_item_add_subtree(ti, ett_ether2);
576
9.34k
    }
577
578
9.42k
    dissect_address_data(tvb, pinfo, fh_tree, true);
579
580
9.42k
    proto_tree_add_uint(fh_tree, hf_eth_type, tvb, 12, 2, ehdr->type);
581
582
9.42k
    ethertype_data.etype = ehdr->type;
583
9.42k
    ethertype_data.payload_offset = ETH_HEADER_SIZE;
584
9.42k
    ethertype_data.fh_tree = fh_tree;
585
9.42k
    ethertype_data.trailer_id = hf_eth_trailer;
586
9.42k
    ethertype_data.fcs_len = fcs_len;
587
588
9.42k
    needs_dissector_with_data = true;
589
9.42k
  }
590
13.6k
  if (header_only)
591
0
    return fh_tree;
592
593
  /* if we still did not leave the dissection, try identifying any ETH conversation
594
   * When deinterlacing was asked and an interface is known, create an _IN conv,
595
   * otherwise create an ordinary _NN one.
596
   *
597
   */
598
599
13.6k
  unsigned conv_type = CONVERSATION_ETH_NN;
600
  /* deinterlacing is requested */
601
13.6k
  if(prefs.conversation_deinterlacing_key>0) {
602
0
    uint32_t dtlc_iface = 0;
603
604
0
    if(prefs.conversation_deinterlacing_key&CONV_DEINT_KEY_INTERFACE &&
605
0
       pinfo->rec->presence_flags & WTAP_HAS_INTERFACE_ID) {
606
607
0
      conv_type = CONVERSATION_ETH_IN;
608
0
      dtlc_iface = pinfo->rec->rec_header.packet_header.interface_id;
609
0
    }
610
0
    else {
611
0
      conv_type = CONVERSATION_ETH_NN;
612
0
    }
613
614
    // identify an existing conversation or create a new one
615
0
    conversation_t *conv_deint = find_conversation_deinterlacer(pinfo->num, &pinfo->src, &pinfo->dst, conv_type,
616
0
                                 dtlc_iface, 0, 0);
617
0
    if(!conv_deint) {
618
0
      conversation_new_deinterlacer(pinfo->num, &pinfo->src, &pinfo->dst,
619
0
                   conv_type, dtlc_iface, 0, 0);
620
0
    }
621
0
  }
622
623
13.6k
  conversation_t *conv = find_conversation(pinfo->num, &pinfo->src, &pinfo->dst, conv_type, 0, 0, NO_PORT_X);
624
625
13.6k
  if(!conv) {
626
3.91k
    conv = conversation_new(pinfo->num, &pinfo->src, &pinfo->dst, conv_type, 0, 0, NO_PORTS);
627
3.91k
  }
628
9.77k
  else {
629
    /*
630
     * while not strictly necessary because there is only 1
631
     * conversation between 2 IPs, we still move the last frame
632
     * indicator as being a usual practice.
633
     */
634
9.77k
    if (!(pinfo->fd->visited)) {
635
9.69k
      if (pinfo->num > conv->last_frame) {
636
6.33k
        conv->last_frame = pinfo->num;
637
6.33k
      }
638
9.69k
    }
639
9.77k
  }
640
641
13.6k
  ethd = get_eth_conversation_data(conv, pinfo);
642
13.6k
  if(ethd) {
643
13.6k
    ehdr->stream = ethd->stream;
644
13.6k
    if(tree) {
645
12.8k
      ti = proto_tree_add_uint(fh_tree, hf_eth_stream, tvb, 0, 0, ethd->stream);
646
12.8k
      proto_item_set_generated(ti);
647
12.8k
    }
648
13.6k
  }
649
650
13.6k
  if(needs_dissector_with_data) {
651
9.34k
    call_dissector_with_data(ethertype_handle, tvb, pinfo, parent_tree, &ethertype_data);
652
9.34k
  }
653
654
13.6k
  return fh_tree;
655
13.6k
}
656
657
static void
658
eth_init(void)
659
15
{
660
15
    eth_stream_count = 0;
661
15
}
662
663
/* -------------- */
664
static bool check_is_802_2(tvbuff_t *tvb, int fcs_len)
665
4.27k
{
666
4.27k
  volatile bool is_802_2;
667
4.27k
  volatile int length;
668
4.27k
  int captured_length, reported_length;
669
670
4.27k
  is_802_2 = true;
671
672
    /* Is there an 802.2 layer? I can tell by looking at the first 2
673
       bytes after the 802.3 header. If they are 0xffff, then what
674
       follows the 802.3 header is an IPX payload, meaning no 802.2.
675
       A non-0xffff value means that there's an 802.2 layer or CCSDS
676
       layer inside the 802.3 layer */
677
678
4.27k
  TRY {
679
4.27k
    if (tvb_get_ntohs(tvb, 14) == 0xffff) {
680
305
        is_802_2 = false;
681
305
    }
682
    /* Is this a CCSDS payload instead of an 802.2 (LLC)?
683
       Check the conditions enabled by the user for CCSDS presence */
684
3.96k
    else if (ccsds_heuristic_length || ccsds_heuristic_version ||
685
3.92k
             ccsds_heuristic_header || ccsds_heuristic_bit) {
686
0
      bool CCSDS_len = true;
687
0
      bool CCSDS_ver = true;
688
0
      bool CCSDS_head = true;
689
0
      bool CCSDS_bit = true;
690
      /* See if the reported payload size matches the
691
         size contained in the CCSDS header. */
692
0
      if (ccsds_heuristic_length) {
693
        /* The following technique to account for FCS
694
           is copied from packet-ieee8023.c dissect_802_3() */
695
0
        length = tvb_get_ntohs(tvb, 12);
696
0
        reported_length = tvb_reported_length_remaining(tvb, ETH_HEADER_SIZE);
697
0
        if (fcs_len > 0) {
698
0
          if (reported_length >= fcs_len)
699
0
            reported_length -= fcs_len;
700
0
        }
701
        /* Make sure the length in the 802.3 header doesn't go past the end of
702
           the payload. */
703
0
        if (length > reported_length) {
704
0
          length = reported_length;
705
0
        }
706
        /* Only allow inspection of 'length' number of bytes. */
707
0
        captured_length = tvb_captured_length_remaining(tvb, ETH_HEADER_SIZE);
708
0
        if (captured_length > length)
709
0
          captured_length = length;
710
711
        /* Check if payload is large enough to contain a CCSDS header */
712
0
        if (captured_length >= 6) {
713
          /* Compare length to packet length contained in CCSDS header. */
714
0
          if (length != 7 + tvb_get_ntohs(tvb, ETH_HEADER_SIZE + 4))
715
0
            CCSDS_len = false;
716
0
        }
717
0
      }
718
      /* Check if CCSDS Version number (first 3 bits of payload) is zero */
719
0
      if ((ccsds_heuristic_version) && (tvb_get_bits8(tvb, 8*ETH_HEADER_SIZE, 3)!=0))
720
0
        CCSDS_ver = false;
721
      /* Check if Secondary Header Flag (4th bit of payload) is set to one. */
722
0
      if ((ccsds_heuristic_header) && (tvb_get_bits8(tvb, 8*ETH_HEADER_SIZE + 4, 1)!=1))
723
0
        CCSDS_head = false;
724
      /* Check if spare bit (1st bit of 7th word of payload) is zero. */
725
0
      if ((ccsds_heuristic_bit) && (tvb_get_bits8(tvb, 8*ETH_HEADER_SIZE + 16*6, 1)!=0))
726
0
        CCSDS_bit = false;
727
      /* If all the conditions are true, don't interpret payload as an 802.2 (LLC).
728
       * Additional check in packet-802.3.c will distinguish between
729
       * IPX and CCSDS packets*/
730
0
      if (CCSDS_len && CCSDS_ver && CCSDS_head && CCSDS_bit)
731
0
        is_802_2 = false;
732
0
    }
733
4.27k
  }
734
4.27k
  CATCH_BOUNDS_ERRORS {
735
37
        ; /* do nothing */
736
737
37
  }
738
4.27k
  ENDTRY;
739
4.27k
  return is_802_2;
740
4.27k
}
741
742
/*
743
 * Add an Ethernet trailer - which, for some captures, might be the FCS
744
 * rather than a pad-to-60-bytes trailer.
745
 *
746
 * If fcs_len is 0, we assume the frame has no FCS; if it's 4, we assume
747
 * it has an FCS; if it's anything else (such as -1, which means "maybe
748
 * it does, maybe it doesn't"), we try to infer whether it has an FCS.
749
 */
750
void
751
add_ethernet_trailer(packet_info *pinfo, proto_tree *tree, proto_tree *fh_tree,
752
    int trailer_id, tvbuff_t *tvb, tvbuff_t *trailer_tvb, int fcs_len,
753
    int payload_offset)
754
13.6k
{
755
  /* If there're some bytes left over, it could be a combination of:
756
     - padding to meet the minimum 64 byte frame length
757
     - an FCS, if present (if fcs_len is 0, we know it's not present;
758
       if fcs_len is 4, we know it's present; if fcs_len is -1, we
759
       need some heuristics to determine whether it's present)
760
     - information inserted by TAPs or other network monitoring equipment.
761
762
     If we don't know whether the FCS is present, then, if we don't have a
763
     network monitoring trailer, and if the Ethernet frame was claimed to
764
     have had 64 or more bytes - i.e., it was at least an FCS worth of data
765
     longer than the minimum payload size - we could assume the last 4 bytes
766
     of the trailer are an FCS. */
767
13.6k
  heur_dtbl_entry_t *hdtbl_entry;
768
769
13.6k
  if (trailer_tvb) {
770
4.85k
    unsigned trailer_length, trailer_reported_length;
771
4.85k
    unsigned padding_length = 0;
772
4.85k
    bool has_fcs = false;
773
4.85k
    bool maybe_padded = false;
774
4.85k
    tvbuff_t *real_trailer_tvb;
775
776
4.85k
    trailer_length = tvb_captured_length(trailer_tvb);
777
4.85k
    trailer_reported_length = tvb_reported_length(trailer_tvb);
778
779
    /* Theoretically padding is added if the frame length without the FCS is
780
     * less than 60 bytes, starting from the addresses. In practice, frames
781
     * are often padded so that the length is 60 bytes not counting any tags
782
     * before the final Ethertype. (I.e., padding so that the payload portion
783
     * is 46.)
784
     *
785
     * Padding might be added to a frame at one point in a network, and then a
786
     * tag or trailer added later without removing the padding. Conversely, a
787
     * frame might have padding and a tag and trailer, and then the tag removed,
788
     * dropping the frame below 60 octets, leading to more padding at the end,
789
     * after the trailer. https://gitlab.com/wireshark/wireshark/-/wikis/PRP
790
     * has useful illustrations of both situations. The heuristic trailer
791
     * dissectors can try to deal with both situations (though looping through
792
     * the trailer bytes increases false positives.)
793
     *
794
     * By increasing the minimum frame size (padding payload to 46) the former
795
     * situation always occurs, and trailers appear at the end. IEEE Std
796
     * 802.1Q-2014 G.2.1 "Treatment of PAD fields in IEEE 802.3 frames"
797
     * and G.2.3 "Minimum PDU size" specifically state it is permissible for a
798
     * Bridge to to adopt a minimum tagged frame length of 68 bytes (64 without
799
     * FCS) when 802.1Q is used. Other specs don't directly address this, but
800
     * we often see padding on frames that are more than 60 octets without FCS.
801
     */
802
4.85k
    int frame_len;
803
4.85k
    if (eth_padding == PADDING_ANY) {
804
      /* This is a size at which there definitely should be padding,
805
       * which we use with PADDING_ANY to be conservative so we don't
806
       * mark any possible trailer as padding. Fo certain cases (tags,
807
       * trailers, especially encapsulation like ISL, GSE Bridged Frames)
808
       * some padding will be classified as trailer.
809
       */
810
0
      frame_len = pinfo->fd->pkt_len;
811
4.85k
    } else {
812
      /* This is the size up to which there might be padding, if padding
813
       * was added before adding tags after the first ethertype.
814
       * Use this if we're testing PADDING_ZERO, which is strict.
815
       * Consecutive zeroes up to this point will be padding,
816
       * anything starting with the first non-zero will be trailer.
817
       */
818
4.85k
      frame_len = tvb_reported_length(tvb) + (14 - payload_offset);
819
4.85k
    }
820
4.85k
    maybe_padded = (frame_len >= 60 && (frame_len - trailer_reported_length) < 60);
821
822
4.85k
    if (eth_padding != PADDING_NONE && maybe_padded) {
823
      /* XXX: There could be another 4 bytes of padding if a Bridge extends
824
       * the minimum frame size of 68 on untagged fraomes, see discussion
825
       * above of IEEE 802.1Q Annex G. If we require padding to be zeros,
826
       * we could possibly use 64 instead of 60. (Too many false positives
827
       * with PADDING_ANY.)
828
       */
829
185
      padding_length = 60 - (frame_len - trailer_reported_length);
830
      /* Require padding to be zeros */
831
185
      if (eth_padding == PADDING_ZEROS) {
832
838
        for (unsigned i = 0; i < padding_length; i++) {
833
811
          if (tvb_get_int8(trailer_tvb, i) != 0) {
834
158
            padding_length = i;
835
158
            break;
836
158
          }
837
811
        }
838
185
      }
839
      /* If it was determined that we have padding, add it to the tree. */
840
185
      if (padding_length > 0) {
841
93
          tvb_ensure_bytes_exist(tvb, 0, padding_length);
842
93
          proto_tree_add_item(fh_tree, hf_eth_padding, trailer_tvb, 0,
843
93
              padding_length, ENC_NA);
844
93
          trailer_length -= padding_length;
845
93
          trailer_reported_length -= padding_length;
846
93
      }
847
185
    }
848
849
4.85k
    int payload_length = tvb_reported_length(tvb) - payload_offset;
850
4.85k
    bool dissected = false;
851
852
4.85k
    if (fcs_len != 4) {
853
      /* Try trailer dissection without an FCS */
854
4.39k
      real_trailer_tvb = tvb_new_subset_remaining(trailer_tvb, padding_length);
855
      /* Call all ethernet trailer dissectors to dissect the trailer if
856
         we actually have a trailer. The PRP trailer dissector wants
857
         to know about the payload (LSDU) length. */
858
4.39k
      if (tvb_reported_length(real_trailer_tvb) != 0) {
859
680
        dissected = dissector_try_heuristic(eth_trailer_subdissector_list,
860
680
                                            real_trailer_tvb, pinfo, tree,
861
680
                                            &hdtbl_entry, &payload_length);
862
680
      }
863
4.39k
    }
864
865
4.85k
    if (fcs_len != 0) {
866
      /* If fcs_len is 4, we assume we definitely have an FCS.
867
         If fcs_len is -1, if the frame is big enough that, if we
868
         have a trailer, it probably includes an FCS, and we have
869
         enough space in the trailer for the FCS, and we didn't
870
         have a heuristic trailer dissector successfully dissect
871
         without an FCS, we assume we have an FCS.
872
873
         "Big enough" means 64 bytes or more; any frame that big
874
         needs no trailer, as there's no need to pad an Ethernet
875
         packet past 60 bytes.
876
877
         XXX: This is not quite true. See IEEE Std 802.1Q-2014
878
         G.2.1 "Treatment of PAD fields in IEEE 802.3 frames" and
879
         G.2.3 "Minimum PDU size" and the discussion above.
880
881
         The trailer must be at least 4 bytes long to have enough
882
         space for an FCS. */
883
884
776
      if (fcs_len == 4 || (fcs_len == -1 && !dissected &&
885
523
        frame_len >= 64 && trailer_reported_length >= 4)) {
886
        /* Either we know we have an FCS, or we believe we have an FCS. */
887
523
        if (trailer_length < trailer_reported_length) {
888
          /* The packet is claimed to have enough data for a 4-byte FCS,
889
             but we didn't capture all of the packet.
890
             Slice off the 4-byte FCS from the reported length, and
891
             trim the captured length so it's no more than the reported
892
             length; that will slice off what of the FCS, if any, is
893
             in the captured packet. */
894
1
          trailer_reported_length -= 4;
895
1
          if (trailer_length > trailer_reported_length) {
896
1
            payload_length -= (trailer_length - trailer_reported_length);
897
1
            trailer_length = trailer_reported_length;
898
1
          }
899
1
          has_fcs = true;
900
522
        } else {
901
          /* We captured all of the packet, including what appears to
902
             be a 4-byte FCS.  Slice it off. */
903
522
          trailer_length -= 4;
904
522
          trailer_reported_length -= 4;
905
522
          payload_length -= 4;
906
522
          has_fcs = true;
907
522
        }
908
909
523
        real_trailer_tvb = tvb_new_subset_length_caplen(trailer_tvb, padding_length,
910
523
                                  trailer_length, trailer_reported_length);
911
912
        /* Call all ethernet trailer dissectors to dissect the trailer if
913
           we actually have a trailer.  */
914
523
        if (tvb_reported_length(real_trailer_tvb) != 0) {
915
95
          dissected = dissector_try_heuristic(eth_trailer_subdissector_list,
916
95
                                              real_trailer_tvb, pinfo, tree,
917
95
                                              &hdtbl_entry, &payload_length);
918
95
        }
919
523
      }
920
776
    }
921
922
4.85k
    if (!dissected) {
923
      /* No luck with the trailer dissectors, so just display the
924
         extra bytes as general trailer */
925
4.81k
      if (trailer_length != 0) {
926
670
        tvb_ensure_bytes_exist(real_trailer_tvb, 0, trailer_length);
927
670
        proto_item *pi = proto_tree_add_item(fh_tree, trailer_id, real_trailer_tvb, 0,
928
670
          trailer_length, ENC_NA);
929
670
        if (maybe_padded) {
930
172
          if (eth_padding == PADDING_ANY && padding_length > 0) {
931
0
            expert_add_info_format(pinfo, pi, &ei_eth_padding_bad,
932
0
                "Padding was assumed, and an undecoded trailer exists. Some of the trailer may have been consumed by padding.");
933
0
          }
934
172
          else if (eth_padding == PADDING_ZEROS && padding_length == 0) {
935
82
            expert_add_info_format(pinfo, pi, &ei_eth_padding_bad,
936
82
                "Didn't find padding of zeros, and an undecoded trailer exists. There may be padding of non-zeros.");
937
82
          }
938
172
        }
939
670
      }
940
4.81k
    }
941
942
4.85k
    if (has_fcs) {
943
521
      uint32_t sent_fcs = tvb_get_ntohl(trailer_tvb, padding_length+trailer_length);
944
      /* If we don't have the entire header, we can't actually check the FCS.
945
       * Dissectors that don't have the entire header (say, a tag) probably
946
       * should have set fcs_len to zero in the ethertype_data struct.
947
       * XXX: Maybe add an expert info saying why we aren't checking the FCS? */
948
521
      if (eth_check_fcs && payload_offset == ETH_HEADER_SIZE) {
949
0
        uint32_t fcs = crc32_802_tvb(tvb, tvb_captured_length(tvb) - 4);
950
0
        proto_tree_add_checksum(fh_tree, trailer_tvb, padding_length+trailer_length, hf_eth_fcs, hf_eth_fcs_status, &ei_eth_fcs_bad, pinfo, fcs, ENC_BIG_ENDIAN, PROTO_CHECKSUM_VERIFY);
951
952
0
        if (fcs != sent_fcs) {
953
0
          col_append_str(pinfo->cinfo, COL_INFO, " [ETHERNET FRAME CHECK SEQUENCE INCORRECT]");
954
0
        }
955
521
      }else{
956
521
        proto_tree_add_checksum(fh_tree, trailer_tvb, padding_length+trailer_length, hf_eth_fcs, hf_eth_fcs_status, &ei_eth_fcs_bad, pinfo, 0, ENC_BIG_ENDIAN, PROTO_CHECKSUM_NO_FLAGS);
957
521
      }
958
521
      trailer_length += 4;
959
521
    }
960
4.85k
    proto_tree_set_appendix(fh_tree, tvb, tvb_captured_length(tvb) - padding_length - trailer_length, padding_length + trailer_length);
961
4.85k
  }
962
13.6k
}
963
964
/* Called for the Ethernet Wiretap encapsulation type; pass the FCS length
965
   reported to us, if known, otherwise falling back to the "fcs" preference. */
966
static int
967
dissect_eth(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
968
2
{
969
2
  struct eth_phdr   *eth = (struct eth_phdr *)data;
970
2
  proto_tree        *fh_tree;
971
2
  tvbuff_t          *real_tvb;
972
2
  int                fcs_len;
973
974
2
  if (eth && eth->fcs_len != -1) {
975
    /* Use the value reported from Wiretap, if known. */
976
0
    fcs_len = eth->fcs_len;
977
2
  } else {
978
2
    fcs_len = eth_fcs;
979
2
  }
980
981
  /* When capturing on a Cisco FEX, some frames (most likely all frames
982
     captured without a vntag) have an extra destination mac prepended. */
983
2
  if (eth_deduplicate_dmac && tvb_captured_length(tvb) > 20 &&
984
0
      memcmp(tvb_get_ptr(tvb,0,6),tvb_get_ptr(tvb,6,6), 6) == 0) {
985
0
    real_tvb = tvb_new_subset_length_caplen(tvb, 6,
986
0
      tvb_captured_length(tvb) - 6, tvb_reported_length(tvb) - 6);
987
2
  } else {
988
2
    real_tvb = tvb;
989
2
  }
990
991
  /* Some devices slice the packet and add their own trailer before
992
     putting the frame on the network. Make sure these packets get
993
     a proper trailer (even though the sliced frame might not
994
     properly dissect. */
995
2
  if ( (eth_trailer_length > 0) && (eth_trailer_length < tvb_captured_length(real_tvb)) ) {
996
0
    tvbuff_t *next_tvb;
997
0
    unsigned total_trailer_length = eth_trailer_length;
998
999
    /* If we have to guess if the trailer includes the FCS, assume not; the
1000
     * user probably set the "eth_trailer_length" preference to the total
1001
     * trailer length. The user has already set the preference, so should
1002
     * have little difficulty changing it or the "fcs" preference if need be.
1003
     */
1004
0
    total_trailer_length += (fcs_len < 0 ? 0 : (unsigned)fcs_len);
1005
1006
    /* Dissect the tvb up to, but not including the trailer */
1007
0
    next_tvb = tvb_new_subset_length_caplen(real_tvb, 0,
1008
0
                              tvb_captured_length(real_tvb) - total_trailer_length,
1009
0
                              tvb_reported_length(real_tvb) - total_trailer_length);
1010
0
    fh_tree = dissect_eth_common(next_tvb, pinfo, tree, 0, false);
1011
1012
    /* Now handle the ethernet trailer and optional FCS */
1013
0
    next_tvb = tvb_new_subset_remaining(real_tvb, tvb_captured_length(real_tvb) - total_trailer_length);
1014
0
    add_ethernet_trailer(pinfo, tree, fh_tree, hf_eth_trailer, real_tvb, next_tvb,
1015
0
                         fcs_len, ETH_HEADER_SIZE);
1016
2
  } else {
1017
2
    dissect_eth_common(real_tvb, pinfo, tree, fcs_len, false);
1018
2
  }
1019
2
  return tvb_captured_length(tvb);
1020
2
}
1021
1022
/* Called by other dissectors  This one's for encapsulated Ethernet
1023
   packets that don't include an FCS. */
1024
static int
1025
dissect_eth_withoutfcs(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
1026
12.6k
{
1027
12.6k
  dissect_eth_common(tvb, pinfo, tree, 0, false);
1028
12.6k
  return tvb_captured_length(tvb);
1029
12.6k
}
1030
1031
1032
/* Called by other dissectors  This one's for Ethernet packet headers */
1033
static int
1034
dissect_eth_header(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
1035
0
{
1036
0
  dissect_eth_common(tvb, pinfo, tree, 0, true);
1037
0
  return tvb_captured_length(tvb);
1038
0
}
1039
1040
/* ...and this one's for encapsulated packets that do. */
1041
static int
1042
dissect_eth_withfcs(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
1043
507
{
1044
507
  dissect_eth_common(tvb, pinfo, tree, 4, false);
1045
507
  return tvb_captured_length(tvb);
1046
507
}
1047
1048
/* ...and this one's for encapsulated packets that might or might not. */
1049
static int
1050
dissect_eth_maybefcs(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
1051
643
{
1052
643
  dissect_eth_common(tvb, pinfo, tree, eth_fcs, false);
1053
643
  return tvb_captured_length(tvb);
1054
643
}
1055
1056
void
1057
proto_register_eth(void)
1058
15
{
1059
15
  register_init_routine(eth_init);
1060
1061
15
  static hf_register_info hf[] = {
1062
1063
15
    { &hf_eth_dst,
1064
15
      { "Destination", "eth.dst", FT_ETHER, BASE_NONE, NULL, 0x0,
1065
15
        "Destination Hardware Address", HFILL }},
1066
1067
15
    { &hf_eth_dst_resolved,
1068
15
      { "Destination (resolved)", "eth.dst_resolved", FT_STRING, BASE_NONE,
1069
15
        NULL, 0x0, "Destination Hardware Address (resolved)", HFILL }},
1070
1071
15
    { &hf_eth_dst_oui,
1072
15
      { "Destination OUI", "eth.dst.oui", FT_UINT24, BASE_OUI,
1073
15
        NULL, 0x0, "Destination Organizationally Unique Identifier", HFILL } },
1074
1075
15
    { &hf_eth_dst_oui_resolved,
1076
15
      { "Destination OUI (resolved)", "eth.dst.oui_resolved", FT_STRING, BASE_NONE,
1077
15
         NULL, 0x0, "Destination Organizationally Unique Identifier (resolved)", HFILL } },
1078
1079
15
    { &hf_eth_src,
1080
15
      { "Source", "eth.src", FT_ETHER, BASE_NONE, NULL, 0x0,
1081
15
        "Source Hardware Address", HFILL }},
1082
1083
15
    { &hf_eth_src_resolved,
1084
15
      { "Source (resolved)", "eth.src_resolved", FT_STRING, BASE_NONE,
1085
15
        NULL, 0x0, "Source Hardware Address (resolved)", HFILL }},
1086
1087
1088
15
    { &hf_eth_src_oui,
1089
15
      { "Source OUI", "eth.src.oui", FT_UINT24, BASE_OUI,
1090
15
        NULL, 0x0, "Source Organizationally Unique Identifier", HFILL } },
1091
1092
15
    { &hf_eth_src_oui_resolved,
1093
15
      { "Source OUI (resolved)", "eth.src.oui_resolved", FT_STRING, BASE_NONE,
1094
15
        NULL, 0x0, "Source Organizationally Unique Identifier (resolved)", HFILL } },
1095
1096
15
    { &hf_eth_len,
1097
15
      { "Length", "eth.len", FT_UINT16, BASE_DEC, NULL, 0x0,
1098
15
        NULL, HFILL }},
1099
1100
    /* registered here but handled in packet-ethertype.c */
1101
15
    { &hf_eth_type,
1102
15
      { "Type", "eth.type", FT_UINT16, BASE_HEX, VALS(etype_vals), 0x0,
1103
15
        NULL, HFILL }},
1104
1105
15
    { &hf_eth_invalid_lentype,
1106
15
      { "Invalid length/type", "eth.invalid_lentype", FT_UINT16, BASE_HEX_DEC,
1107
15
        NULL, 0x0, NULL, HFILL }},
1108
1109
15
    { &hf_eth_addr,
1110
15
      { "Address", "eth.addr", FT_ETHER, BASE_NONE, NULL, 0x0,
1111
15
        "Source or Destination Hardware Address", HFILL }},
1112
1113
15
    { &hf_eth_addr_resolved,
1114
15
      { "Address (resolved)", "eth.addr_resolved", FT_STRING, BASE_NONE,
1115
15
        NULL, 0x0, "Source or Destination Hardware Address (resolved)",
1116
15
        HFILL }},
1117
1118
15
    { &hf_eth_addr_oui,
1119
15
      { "Address OUI", "eth.addr.oui", FT_UINT24, BASE_OUI,
1120
15
        NULL, 0x0, "Address Organizationally Unique Identifier", HFILL } },
1121
1122
15
    { &hf_eth_addr_oui_resolved,
1123
15
      { "Address OUI (resolved)", "eth.addr.oui_resolved", FT_STRING, BASE_NONE,
1124
15
        NULL, 0x0, "Address Organizationally Unique Identifier (resolved)", HFILL } },
1125
1126
15
    { &hf_eth_padding,
1127
15
      { "Padding", "eth.padding", FT_BYTES, BASE_NONE, NULL, 0x0,
1128
15
        "Ethernet Padding", HFILL }},
1129
1130
15
    { &hf_eth_trailer,
1131
15
      { "Trailer", "eth.trailer", FT_BYTES, BASE_NONE, NULL, 0x0,
1132
15
        "Ethernet Trailer or Checksum", HFILL }},
1133
1134
15
    { &hf_eth_fcs,
1135
15
      { "Frame check sequence", "eth.fcs", FT_UINT32, BASE_HEX, NULL, 0x0,
1136
15
        "Ethernet checksum", HFILL }},
1137
1138
15
    { &hf_eth_fcs_status,
1139
15
      { "FCS Status", "eth.fcs.status", FT_UINT8, BASE_NONE, VALS(proto_checksum_vals), 0x0,
1140
15
        NULL, HFILL }},
1141
1142
15
    { &hf_eth_dst_lg,
1143
15
      { "LG bit", "eth.dst.lg", FT_BOOLEAN, 24,
1144
15
        TFS(&lg_tfs), 0x020000,
1145
15
        "Specifies if this is a locally administered or globally unique (IEEE assigned) address", HFILL }},
1146
1147
15
    { &hf_eth_dst_ig,
1148
15
      { "IG bit", "eth.dst.ig", FT_BOOLEAN, 24,
1149
15
        TFS(&ig_tfs), 0x010000,
1150
15
        "Specifies if this is an individual (unicast) or group (broadcast/multicast) address", HFILL }},
1151
1152
15
    { &hf_eth_src_lg,
1153
15
      { "LG bit", "eth.src.lg", FT_BOOLEAN, 24,
1154
15
        TFS(&lg_tfs), 0x020000,
1155
15
        "Specifies if this is a locally administered or globally unique (IEEE assigned) address", HFILL }},
1156
1157
15
    { &hf_eth_src_ig,
1158
15
      { "IG bit", "eth.src.ig", FT_BOOLEAN, 24,
1159
15
        TFS(&ig_tfs), 0x010000,
1160
15
        "Specifies if this is an individual (unicast) or group (broadcast/multicast) address", HFILL }},
1161
1162
15
    { &hf_eth_lg,
1163
15
      { "LG bit", "eth.lg", FT_BOOLEAN, 24,
1164
15
        TFS(&lg_tfs), 0x020000,
1165
15
        "Specifies if this is a locally administered or globally unique (IEEE assigned) address", HFILL }},
1166
1167
15
    { &hf_eth_ig,
1168
15
      { "IG bit", "eth.ig", FT_BOOLEAN, 24,
1169
15
        TFS(&ig_tfs), 0x010000,
1170
15
        "Specifies if this is an individual (unicast) or group (broadcast/multicast) address", HFILL }},
1171
1172
15
    { &hf_eth_stream,
1173
15
      { "Stream index", "eth.stream", FT_UINT32, BASE_DEC, NULL, 0x0,
1174
15
        NULL, HFILL }}
1175
15
  };
1176
15
  static int *ett[] = {
1177
15
    &ett_ieee8023,
1178
15
    &ett_ether2,
1179
15
    &ett_ether,
1180
15
    &ett_addr,
1181
15
  };
1182
1183
15
  static ei_register_info ei[] = {
1184
15
    { &ei_eth_invalid_lentype, { "eth.invalid_lentype.expert", PI_PROTOCOL, PI_WARN, "Invalid length/type", EXPFILL }},
1185
15
    { &ei_eth_src_not_group, { "eth.src_not_group", PI_PROTOCOL, PI_WARN, "Source MAC must not be a group address: IEEE 802.3-2002, Section 3.2.3(b)", EXPFILL }},
1186
15
    { &ei_eth_fcs_bad, { "eth.fcs_bad", PI_CHECKSUM, PI_ERROR, "Bad checksum", EXPFILL }},
1187
15
    { &ei_eth_len, { "eth.len.past_end", PI_MALFORMED, PI_ERROR, "Length field value goes past the end of the payload", EXPFILL }},
1188
15
    { &ei_eth_padding_bad, {"eth.padding_bad", PI_PROTOCOL, PI_NOTE, "Padding identification may be inaccurate and impact trailer dissector", EXPFILL }},
1189
15
  };
1190
1191
15
  module_t *eth_module;
1192
15
  expert_module_t* expert_eth;
1193
1194
15
  proto_eth = proto_register_protocol("Ethernet", "Ethernet", "eth");
1195
15
  proto_register_field_array(proto_eth, hf, array_length(hf));
1196
15
  proto_register_subtree_array(ett, array_length(ett));
1197
15
  expert_eth = expert_register_protocol(proto_eth);
1198
15
  expert_register_field_array(expert_eth, ei, array_length(ei));
1199
1200
  /* subdissector code */
1201
15
  heur_subdissector_list = register_heur_dissector_list_with_description("eth", "Ethernet framed non-Ethernet data", proto_eth);
1202
15
  eth_trailer_subdissector_list = register_heur_dissector_list_with_description("eth.trailer", "Ethernet trailer", proto_eth);
1203
1204
  /* Register configuration preferences */
1205
15
  eth_module = prefs_register_protocol(proto_eth, NULL);
1206
1207
15
  prefs_register_obsolete_preference(eth_module, "assume_padding");
1208
15
  prefs_register_enum_preference(eth_module, "padding",
1209
15
                                 "Assume padding for short frames with trailer",
1210
15
                                 "Some devices add trailing data to frames.  Depending on where this "
1211
15
                                 "device exists in the network, padding could be added to short "
1212
15
                                 "frames before the additional trailer.  This option determines how "
1213
15
                                 "that padding will be detected.\n\n"
1214
15
                                 "Never - Don't detect any padding.  Any bytes after the ethernet "
1215
15
                                 "payload will be considered trailer.\n"
1216
15
                                 "Zeros (default) - Consecutive bytes of zeros up to the minimum "
1217
15
                                 "ethernet frame size will be treated as padding.  Additional bytes will "
1218
15
                                 "be considered trailer.\n"
1219
15
                                 "Any - Any bytes after the payload up to the minimum ethernet frame "
1220
15
                                 "size will be treated as padding.  Additional bytes will be considered "
1221
15
                                 "trailer.",
1222
15
                                 &eth_padding, eth_padding_vals, false);
1223
1224
15
  prefs_register_uint_preference(eth_module, "trailer_length",
1225
15
                                 "Fixed ethernet trailer length",
1226
15
                                 "Some TAPs add a fixed length ethernet trailer at the end "
1227
15
                                 "of the frame, but before the (optional) FCS. Make sure it "
1228
15
                                 "gets interpreted correctly.",
1229
15
                                 10, &eth_trailer_length);
1230
1231
15
  prefs_register_obsolete_preference(eth_module, "assume_fcs");
1232
15
  prefs_register_enum_preference(eth_module, "fcs",
1233
15
                                 "Assume packets have FCS",
1234
15
                                 "Some Ethernet adapters and drivers include the FCS at the end of a packet, others do not.  "
1235
15
                                 "Some capture file formats and protocols do not indicate whether or not the FCS is included. "
1236
15
                                 "The Ethernet dissector then attempts to guess whether a captured packet has an FCS, "
1237
15
                                 "but it cannot always guess correctly.  This option can override that heuristic "
1238
15
                                 "and assume that the FCS is either never or always present in such cases.",
1239
15
                                 &eth_fcs, eth_fcs_vals, false);
1240
1241
15
  prefs_register_bool_preference(eth_module, "check_fcs",
1242
15
                                 "Validate the Ethernet checksum if possible",
1243
15
                                 "Whether to validate the Frame Check Sequence",
1244
15
                                 &eth_check_fcs);
1245
1246
15
  prefs_register_bool_preference(eth_module, "interpret_as_fw1_monitor",
1247
15
                                 "Attempt to interpret as FireWall-1 monitor file",
1248
15
                                 "Whether packets should be interpreted as coming from CheckPoint FireWall-1 monitor file if they look as if they do",
1249
15
                                 &eth_interpret_as_fw1_monitor);
1250
1251
15
  prefs_register_bool_preference(eth_module, "deduplicate_dmac",
1252
15
                                 "Skip bytes 1-6 if identical to 7-12",
1253
15
                                 "When capturing on a Cisco FEX some frames start with an extra destination mac",
1254
15
                                 &eth_deduplicate_dmac);
1255
1256
15
  prefs_register_static_text_preference(eth_module, "ccsds_heuristic",
1257
15
                                        "Dissect as CCSDS if",
1258
15
                                        "These are the conditions to match a payload against in order to determine if this\n"
1259
15
                                        "is a CCSDS (Consultative Committee for Space Data Systems) packet within\n"
1260
15
                                        "an 802.3 packet. A packet is considered as a possible CCSDS packet only if\n"
1261
15
                                        "one or more of the conditions are checked.");
1262
1263
15
  prefs_register_bool_preference(eth_module, "ccsds_heuristic_length",
1264
15
                                 "CCSDS Length in header matches payload size",
1265
15
                                 "Set the condition that must be true for the CCSDS dissector to be called",
1266
15
                                 &ccsds_heuristic_length);
1267
1268
15
  prefs_register_bool_preference(eth_module, "ccsds_heuristic_version",
1269
15
                                 "CCSDS Version # is zero",
1270
15
                                 "Set the condition that must be true for the CCSDS dissector to be called",
1271
15
                                 &ccsds_heuristic_version);
1272
1273
15
  prefs_register_bool_preference(eth_module, "ccsds_heuristic_header",
1274
15
                                 "CCSDS Secondary Header Flag is set",
1275
15
                                 "Set the condition that must be true for the CCSDS dissector to be called",
1276
15
                                 &ccsds_heuristic_header);
1277
1278
15
  prefs_register_bool_preference(eth_module, "ccsds_heuristic_bit",
1279
15
                                 "CCSDS Spare bit is cleared",
1280
15
                                 "Set the condition that must be true for the CCSDS dissector to be called",
1281
15
                                 &ccsds_heuristic_bit);
1282
1283
15
  eth_header_only_handle = register_dissector("eth_header", dissect_eth_header, proto_eth);
1284
15
  eth_withoutfcs_handle = register_dissector("eth_withoutfcs", dissect_eth_withoutfcs, proto_eth);
1285
15
  register_dissector("eth_withfcs", dissect_eth_withfcs, proto_eth);
1286
15
  eth_maybefcs_handle = register_dissector("eth_maybefcs", dissect_eth_maybefcs, proto_eth);
1287
15
  eth_tap = register_tap("eth");
1288
1289
15
  register_conversation_table(proto_eth, true, eth_conversation_packet, eth_endpoint_packet);
1290
15
  register_conversation_filter("eth", "Ethernet", eth_filter_valid, eth_build_filter, NULL);
1291
1292
15
  register_capture_dissector("eth", capture_eth, proto_eth);
1293
15
}
1294
1295
void
1296
proto_reg_handoff_eth(void)
1297
15
{
1298
15
  dissector_handle_t eth_handle;
1299
15
  capture_dissector_handle_t eth_cap_handle;
1300
1301
  /* Get a handle for the Firewall-1 dissector. */
1302
15
  fw1_handle = find_dissector_add_dependency("fw1", proto_eth);
1303
1304
  /* Get a handle for the ethertype dissector. */
1305
15
  ethertype_handle = find_dissector_add_dependency("ethertype", proto_eth);
1306
1307
15
  eth_handle = create_dissector_handle(dissect_eth, proto_eth);
1308
15
  dissector_add_uint("wtap_encap", WTAP_ENCAP_ETHERNET, eth_handle);
1309
  /* This needs a different (& more user-friendly) name than the other tap */
1310
15
  exported_pdu_tap = register_export_pdu_tap_with_encap("Ethernet", WTAP_ENCAP_ETHERNET);
1311
1312
15
  dissector_add_uint("ethertype", ETHERTYPE_ETHBRIDGE, eth_withoutfcs_handle);
1313
1314
15
  dissector_add_uint("erf.types.type", ERF_TYPE_ETH, eth_maybefcs_handle);
1315
15
  dissector_add_uint("erf.types.type", ERF_TYPE_COLOR_ETH, eth_maybefcs_handle);
1316
15
  dissector_add_uint("erf.types.type", ERF_TYPE_DSM_COLOR_ETH, eth_maybefcs_handle);
1317
15
  dissector_add_uint("erf.types.type", ERF_TYPE_COLOR_HASH_ETH, eth_maybefcs_handle);
1318
15
  dissector_add_uint("ip.proto", IP_PROTO_ETHERNET, eth_maybefcs_handle);
1319
1320
15
  dissector_add_uint("chdlc.protocol", ETHERTYPE_ETHBRIDGE, eth_withoutfcs_handle);
1321
15
  dissector_add_for_decode_as("gre.subproto", eth_withoutfcs_handle);
1322
15
  dissector_add_uint("gre.proto", ETHERTYPE_ETHBRIDGE, eth_withoutfcs_handle);
1323
15
  dissector_add_uint("gre.proto", GRE_MIKROTIK_EOIP, eth_withoutfcs_handle);
1324
15
  dissector_add_uint("juniper.proto", JUNIPER_PROTO_ETHER, eth_withoutfcs_handle);
1325
15
  dissector_add_uint("sflow_245.header_protocol", SFLOW_245_HEADER_ETHERNET, eth_withoutfcs_handle);
1326
15
  dissector_add_uint("l2tp.pw_type", L2TPv3_PW_ETH, eth_withoutfcs_handle);
1327
15
  dissector_add_uint("vxlan.next_proto", VXLAN_ETHERNET, eth_withoutfcs_handle);
1328
15
  dissector_add_uint("sll.ltype", LINUX_SLL_P_ETHERNET, eth_withoutfcs_handle);
1329
15
  dissector_add_uint("nsh.next_proto", NSH_ETHERNET, eth_withoutfcs_handle);
1330
1331
15
  dissector_add_uint("acdr.media_type", ACDR_Control, eth_withoutfcs_handle);
1332
15
  dissector_add_uint("acdr.media_type", ACDR_DSP_SNIFFER, eth_withoutfcs_handle);
1333
15
  dissector_add_uint("mctp.encap-type", MCTP_TYPE_ETHERNET, eth_withoutfcs_handle);
1334
1335
  /*
1336
   * This is to handle the output for the Cisco CMTS "cable intercept"
1337
   * command - it encapsulates Ethernet frames in UDP packets, but
1338
   * the UDP port is user-defined.
1339
   */
1340
15
  dissector_add_for_decode_as_with_preference("udp.port", eth_withoutfcs_handle);
1341
1342
15
  dissector_add_for_decode_as("pcli.payload", eth_withoutfcs_handle);
1343
1344
15
  eth_cap_handle = find_capture_dissector("eth");
1345
15
  capture_dissector_add_uint("wtap_encap", WTAP_ENCAP_ETHERNET, eth_cap_handle);
1346
15
  capture_dissector_add_uint("atm_lane", TRAF_ST_LANE_802_3, eth_cap_handle);
1347
15
  capture_dissector_add_uint("atm_lane", TRAF_ST_LANE_802_3_MC, eth_cap_handle);
1348
15
  capture_dissector_add_uint("ppi", 1 /* DLT_EN10MB */, eth_cap_handle);
1349
15
  capture_dissector_add_uint("sll.ltype", LINUX_SLL_P_ETHERNET, eth_cap_handle);
1350
1351
15
  isl_cap_handle = find_capture_dissector("isl");
1352
15
  ipx_cap_handle = find_capture_dissector("ipx");
1353
15
  llc_cap_handle = find_capture_dissector("llc");
1354
15
}
1355
1356
/*
1357
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
1358
 *
1359
 * Local Variables:
1360
 * c-basic-offset: 2
1361
 * tab-width: 8
1362
 * indent-tabs-mode: nil
1363
 * End:
1364
 *
1365
 * ex: set shiftwidth=2 tabstop=8 expandtab:
1366
 * :indentSize=2:tabSize=8:noTabs=true:
1367
 */