aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-socketcan.c
blob: 3f207480497092e8616e42de8310468e53dcb80d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
/* packet-socketcan.c
 * Routines for disassembly of packets from SocketCAN
 * Felix Obenhuber <felix@obenhuber.de>
 *
 * Added support for the DeviceNet Dissector
 * Hans-Joergen Gunnarsson <hag@hms.se>
 * Copyright 2013
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/prefs.h>
#include <epan/expert.h>
#include <epan/decode_as.h>
#include <wiretap/wtap.h>

#include "packet-sll.h"
#include "packet-socketcan.h"

void proto_register_socketcan(void);
void proto_reg_handoff_socketcan(void);

static int hf_can_len = -1;
static int hf_can_ident_ext = -1;
static int hf_can_ident_std = -1;
static int hf_can_extflag = -1;
static int hf_can_rtrflag = -1;
static int hf_can_errflag = -1;
static int hf_can_reserved = -1;
static int hf_can_padding = -1;

static int hf_can_err_tx_timeout = -1;
static int hf_can_err_lostarb = -1;
static int hf_can_err_ctrl = -1;
static int hf_can_err_prot = -1;
static int hf_can_err_trx = -1;
static int hf_can_err_ack = -1;
static int hf_can_err_busoff = -1;
static int hf_can_err_buserror = -1;
static int hf_can_err_restarted = -1;
static int hf_can_err_reserved = -1;

static int hf_can_err_lostarb_bit_number = -1;

static int hf_can_err_ctrl_rx_overflow = -1;
static int hf_can_err_ctrl_tx_overflow = -1;
static int hf_can_err_ctrl_rx_warning = -1;
static int hf_can_err_ctrl_tx_warning = -1;
static int hf_can_err_ctrl_rx_passive = -1;
static int hf_can_err_ctrl_tx_passive = -1;
static int hf_can_err_ctrl_active = -1;

static int hf_can_err_prot_error_type_bit = -1;
static int hf_can_err_prot_error_type_form = -1;
static int hf_can_err_prot_error_type_stuff = -1;
static int hf_can_err_prot_error_type_bit0 = -1;
static int hf_can_err_prot_error_type_bit1 = -1;
static int hf_can_err_prot_error_type_overload = -1;
static int hf_can_err_prot_error_type_active = -1;
static int hf_can_err_prot_error_type_tx = -1;

static int hf_can_err_prot_error_location = -1;

static int hf_can_err_trx_canh = -1;
static int hf_can_err_trx_canl = -1;

static int hf_can_err_ctrl_specific = -1;

static expert_field ei_can_err_dlc_mismatch = EI_INIT;

static int hf_canfd_brsflag = -1;
static int hf_canfd_esiflag = -1;

static gint ett_can = -1;
static gint ett_can_fd = -1;

static int proto_can = -1;
static int proto_canfd = -1;

static gboolean byte_swap = FALSE;

#define LINUX_CAN_STD   0
#define LINUX_CAN_EXT   1
#define LINUX_CAN_ERR   2

#define CAN_LEN_OFFSET     4
#define CAN_DATA_OFFSET    8

#define CANFD_FLAG_OFFSET  5

#define CANFD_BRS 0x01 /* bit rate switch (second bitrate for payload data) */
#define CANFD_ESI 0x02 /* error state indicator of the transmitting node */

static dissector_table_t subdissector_table;
static dissector_handle_t socketcan_bigendian_handle;
static dissector_handle_t socketcan_hostendian_handle;
static dissector_handle_t socketcan_fd_handle;

static const value_string frame_type_vals[] =
{
	{ LINUX_CAN_STD, "STD" },
	{ LINUX_CAN_EXT, "XTD" },
	{ LINUX_CAN_ERR, "ERR" },
	{ 0, NULL }
};

static const value_string can_err_prot_error_location_vals[] =
{
	{ 0x00, "unspecified" },
	{ 0x02, "ID bits 28 - 21 (SFF: 10 - 3)" },
	{ 0x03, "start of frame" },
	{ 0x04, "substitute RTR (SFF: RTR)" },
	{ 0x05, "identifier extension" },
	{ 0x06, "ID bits 20 - 18 (SFF: 2 - 0)" },
	{ 0x07, "ID bits 17-13" },
	{ 0x08, "CRC sequence" },
	{ 0x09, "reserved bit 0" },
	{ 0x0A, "data section" },
	{ 0x0B, "data length code" },
	{ 0x0C, "RTR" },
	{ 0x0D, "reserved bit 1" },
	{ 0x0E, "ID bits 4-0" },
	{ 0x0F, "ID bits 12-5" },
	{ 0x12, "intermission" },
	{ 0x18, "CRC delimiter" },
	{ 0x19, "ACK slot" },
	{ 0x1A, "end of frame" },
	{ 0x1B, "ACK delimiter" },
	{ 0, NULL }
};

static const value_string can_err_trx_canh_vals[] =
{
	{ 0x00, "unspecified" },
	{ 0x04, "no wire" },
	{ 0x05, "short to BAT" },
	{ 0x06, "short to VCC" },
	{ 0x07, "short to GND" },
	{ 0, NULL }
};

static const value_string can_err_trx_canl_vals[] =
{
	{ 0x00, "unspecified" },
	{ 0x04, "no wire" },
	{ 0x05, "short to BAT" },
	{ 0x06, "short to VCC" },
	{ 0x07, "short to GND" },
	{ 0x08, "short to CANH" },
	{ 0, NULL }
};

static int
dissect_socketcan_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
						guint encoding)
{
	proto_tree *can_tree;
	proto_item *ti;
	guint8      frame_type;
	gint        frame_len;
	struct can_identifier can_id;
	const int **can_flags;

	static const int *can_std_flags[] = {
		&hf_can_ident_std,
		&hf_can_extflag,
		&hf_can_rtrflag,
		&hf_can_errflag,
		NULL,
	};
	static const int *can_ext_flags[] = {
		&hf_can_ident_ext,
		&hf_can_extflag,
		&hf_can_rtrflag,
		&hf_can_errflag,
		NULL,
	};
	static const int *can_err_flags[] = {
		&hf_can_errflag,
		&hf_can_err_tx_timeout,
		&hf_can_err_lostarb,
		&hf_can_err_ctrl,
		&hf_can_err_prot,
		&hf_can_err_trx,
		&hf_can_err_ack,
		&hf_can_err_busoff,
		&hf_can_err_buserror,
		&hf_can_err_restarted,
		&hf_can_err_reserved,
		NULL,
	};

	can_id.id = tvb_get_guint32(tvb, 0, encoding);
	frame_len = tvb_get_guint8(tvb, CAN_LEN_OFFSET);

	/* Error Message Frames are only encapsulated in Classic CAN frames */
	if (can_id.id & CAN_ERR_FLAG)
	{
		frame_type = LINUX_CAN_ERR;
		can_flags  = can_err_flags;
	}
	else if (can_id.id & CAN_EFF_FLAG)
	{
		frame_type = LINUX_CAN_EXT;
		can_id.id &= (CAN_EFF_MASK | CAN_FLAG_MASK);
		can_flags  = can_ext_flags;
	}
	else
	{
		frame_type = LINUX_CAN_STD;
		can_id.id &= (CAN_SFF_MASK | CAN_FLAG_MASK);
		can_flags  = can_std_flags;
	}

	col_set_str(pinfo->cinfo, COL_PROTOCOL, "CAN");
	col_clear(pinfo->cinfo, COL_INFO);

	ti = proto_tree_add_item(tree, proto_can, tvb, 0, -1, ENC_NA);
	can_tree = proto_item_add_subtree(ti, ett_can);

	proto_tree_add_bitmask_list(can_tree, tvb, 0, 4, can_flags, encoding);
	proto_tree_add_item(can_tree, hf_can_len, tvb, CAN_LEN_OFFSET, 1, ENC_NA);
	if (frame_type == LINUX_CAN_ERR && frame_len != CAN_ERR_DLC)
	{
		proto_tree_add_expert(tree, pinfo, &ei_can_err_dlc_mismatch, tvb, CAN_LEN_OFFSET, 1);
	}
	proto_tree_add_item(can_tree, hf_can_reserved, tvb, CAN_LEN_OFFSET+1, 3, ENC_NA);

	if (frame_type == LINUX_CAN_ERR)
	{
		const int **flag;
		const char *sepa = ": ";

		col_set_str(pinfo->cinfo, COL_INFO, "ERR");

		for (flag = can_err_flags; *flag; flag++)
		{
			header_field_info *hfi;

			hfi = proto_registrar_get_nth(**flag);
			if (!hfi)
				continue;

			if ((can_id.id & hfi->bitmask & ~CAN_FLAG_MASK) == 0)
				continue;

			col_append_sep_str(pinfo->cinfo, COL_INFO, sepa, hfi->name);
			sepa = ", ";
		}

		if (can_id.id & CAN_ERR_LOSTARB)
			proto_tree_add_item(can_tree, hf_can_err_lostarb_bit_number, tvb, CAN_DATA_OFFSET+0, 1, ENC_NA);
		if (can_id.id & CAN_ERR_CTRL)
		{
			static const int *can_err_ctrl_flags[] = {
				&hf_can_err_ctrl_rx_overflow,
				&hf_can_err_ctrl_tx_overflow,
				&hf_can_err_ctrl_rx_warning,
				&hf_can_err_ctrl_tx_warning,
				&hf_can_err_ctrl_rx_passive,
				&hf_can_err_ctrl_tx_passive,
				&hf_can_err_ctrl_active,
				NULL,
			};

			proto_tree_add_bitmask_list(can_tree, tvb, CAN_DATA_OFFSET+1, 1, can_err_ctrl_flags, ENC_NA);
		}
		if (can_id.id & CAN_ERR_PROT)
		{
			static const int *can_err_prot_error_type_flags[] = {
				&hf_can_err_prot_error_type_bit,
				&hf_can_err_prot_error_type_form,
				&hf_can_err_prot_error_type_stuff,
				&hf_can_err_prot_error_type_bit0,
				&hf_can_err_prot_error_type_bit1,
				&hf_can_err_prot_error_type_overload,
				&hf_can_err_prot_error_type_active,
				&hf_can_err_prot_error_type_tx,
				NULL
			};
			proto_tree_add_bitmask_list(can_tree, tvb, CAN_DATA_OFFSET+2, 1, can_err_prot_error_type_flags, ENC_NA);
			proto_tree_add_item(can_tree, hf_can_err_prot_error_location, tvb, CAN_DATA_OFFSET+3, 1, ENC_NA);
		}
		if (can_id.id & CAN_ERR_TRX)
		{
			proto_tree_add_item(can_tree, hf_can_err_trx_canh, tvb, CAN_DATA_OFFSET+4, 1, ENC_NA);
			proto_tree_add_item(can_tree, hf_can_err_trx_canl, tvb, CAN_DATA_OFFSET+4, 1, ENC_NA);
		}
		proto_tree_add_item(can_tree, hf_can_err_ctrl_specific, tvb, CAN_DATA_OFFSET+5, 3, ENC_NA);
	}
	else
	{
		tvbuff_t   *next_tvb;

		col_add_fstr(pinfo->cinfo, COL_INFO, "%s: 0x%08x   ",
			     val_to_str(frame_type, frame_type_vals, "Unknown (0x%02x)"), (can_id.id & ~CAN_FLAG_MASK));

		if (can_id.id & CAN_RTR_FLAG)
		{
			col_append_str(pinfo->cinfo, COL_INFO, "(Remote Transmission Request)");
		}
		else
		{
			col_append_str(pinfo->cinfo, COL_INFO, tvb_bytes_to_str_punct(wmem_packet_scope(), tvb, CAN_DATA_OFFSET, frame_len, ' '));
		}

		next_tvb = tvb_new_subset_length(tvb, CAN_DATA_OFFSET, frame_len);
		if (!dissector_try_payload_new(subdissector_table, next_tvb, pinfo, tree, TRUE, &can_id))
		{
			call_data_dissector(next_tvb, pinfo, tree);
		}
	}

	if (tvb_captured_length_remaining(tvb, CAN_DATA_OFFSET+frame_len) > 0)
	{
		proto_tree_add_item(can_tree, hf_can_padding, tvb, CAN_DATA_OFFSET+frame_len, -1, ENC_NA);
	}

	return tvb_captured_length(tvb);
}

static int
dissect_socketcan_bigendian(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
    void* data _U_)
{
	return dissect_socketcan_common(tvb, pinfo, tree,
	    byte_swap ? ENC_LITTLE_ENDIAN : ENC_BIG_ENDIAN);
}

#if G_BYTE_ORDER == G_LITTLE_ENDIAN
    #define ENC_ANTI_HOST_ENDIAN ENC_BIG_ENDIAN
#else
    #define ENC_ANTI_HOST_ENDIAN ENC_LITTLE_ENDIAN
#endif

static int
dissect_socketcan_hostendian(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
    void* data _U_)
{
	return dissect_socketcan_common(tvb, pinfo, tree,
	    byte_swap ? ENC_ANTI_HOST_ENDIAN : ENC_HOST_ENDIAN);
}

static int
dissect_socketcanfd_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
							guint encoding)
{
	proto_tree *can_tree;
	proto_item *ti;
	guint8      frame_type;
	gint        frame_len;
	struct can_identifier can_id;
	tvbuff_t*   next_tvb;
	int * can_flags_fd[] = {
		&hf_can_ident_ext,
		&hf_can_extflag,
		NULL,
	};
	static const int * canfd_flag_fields[] = {
		&hf_canfd_brsflag,
		&hf_canfd_esiflag,
		NULL,
	};

	can_id.id = tvb_get_guint32(tvb, 0, encoding);
	frame_len = tvb_get_guint8(tvb, CAN_LEN_OFFSET);

	if (can_id.id & CAN_EFF_FLAG)
	{
		frame_type = LINUX_CAN_EXT;
		can_id.id &= (CAN_EFF_MASK | CAN_FLAG_MASK);
	}
	else
	{
		frame_type = LINUX_CAN_STD;
		can_id.id &= (CAN_SFF_MASK | CAN_FLAG_MASK);
		can_flags_fd[0] = &hf_can_ident_std;
	}

	col_set_str(pinfo->cinfo, COL_PROTOCOL, "CANFD");
	col_clear(pinfo->cinfo, COL_INFO);

	col_add_fstr(pinfo->cinfo, COL_INFO, "%s: 0x%08x   %s",
		     val_to_str(frame_type, frame_type_vals, "Unknown (0x%02x)"), (can_id.id & ~CAN_FLAG_MASK),
		     tvb_bytes_to_str_punct(wmem_packet_scope(), tvb, CAN_DATA_OFFSET, frame_len, ' '));

	ti = proto_tree_add_item(tree, proto_canfd, tvb, 0, -1, ENC_NA);
	can_tree = proto_item_add_subtree(ti, ett_can_fd);

	proto_tree_add_bitmask_list(can_tree, tvb, 0, 4, (const int**)can_flags_fd, encoding);

	proto_tree_add_item(can_tree, hf_can_len, tvb, CAN_LEN_OFFSET, 1, ENC_NA);
	proto_tree_add_bitmask_list(can_tree, tvb, CANFD_FLAG_OFFSET, 1, canfd_flag_fields, ENC_NA);
	proto_tree_add_item(can_tree, hf_can_reserved, tvb, CANFD_FLAG_OFFSET+1, 2, ENC_NA);

	next_tvb = tvb_new_subset_length(tvb, CAN_DATA_OFFSET, frame_len);

	if (!dissector_try_payload_new(subdissector_table, next_tvb, pinfo, tree, TRUE, &can_id))
	{
		call_data_dissector(next_tvb, pinfo, tree);
	}

	if (tvb_captured_length_remaining(tvb, CAN_DATA_OFFSET+frame_len) > 0)
	{
		proto_tree_add_item(can_tree, hf_can_padding, tvb, CAN_DATA_OFFSET+frame_len, -1, ENC_NA);
	}

	return tvb_captured_length(tvb);
}

static int
dissect_socketcan_fd(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
    void* data _U_)
{
	return dissect_socketcanfd_common(tvb, pinfo, tree,
	    byte_swap ? ENC_ANTI_HOST_ENDIAN : ENC_HOST_ENDIAN);
}

void
proto_register_socketcan(void)
{
	static hf_register_info hf[] = {
		{
			&hf_can_ident_ext,
			{
				"Identifier", "can.id",
				FT_UINT32, BASE_HEX,
				NULL, CAN_EFF_MASK,
				NULL, HFILL
			}
		},
		{
			&hf_can_ident_std,
			{
				"Identifier", "can.id",
				FT_UINT32, BASE_HEX,
				NULL, CAN_SFF_MASK,
				NULL, HFILL
			}
		},
		{
			&hf_can_extflag,
			{
				"Extended Flag", "can.flags.xtd",
				FT_BOOLEAN, 32,
				NULL, CAN_EFF_FLAG,
				NULL, HFILL
			}
		},
		{
			&hf_can_rtrflag,
			{
				"Remote Transmission Request Flag", "can.flags.rtr",
				FT_BOOLEAN, 32,
				NULL, CAN_RTR_FLAG,
				NULL, HFILL
			}
		},
		{
			&hf_can_errflag,
			{
				"Error Message Flag", "can.flags.err",
				FT_BOOLEAN, 32,
				NULL, CAN_ERR_FLAG,
				NULL, HFILL
			}
		},
		{
			&hf_can_len,
			{
				"Frame-Length", "can.len",
				FT_UINT8, BASE_DEC,
				NULL, 0x0,
				NULL, HFILL
			}
		},
		{
			&hf_can_reserved,
			{
				"Reserved", "can.reserved",
				FT_BYTES, BASE_NONE,
				NULL, 0x0,
				NULL, HFILL
			}
		},
		{
			&hf_can_padding,
			{
				"Padding", "can.padding",
				FT_BYTES, BASE_NONE,
				NULL, 0x0,
				NULL, HFILL
			}
		},
		{
			&hf_canfd_brsflag,
			{
				"Bit Rate Setting", "canfd.flags.brs",
				FT_BOOLEAN, 8,
				NULL, CANFD_BRS,
				NULL, HFILL
			}
		},
		{
			&hf_canfd_esiflag,
			{
				"Error State Indicator", "canfd.flags.esi",
				FT_BOOLEAN, 8,
				NULL, CANFD_ESI,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_tx_timeout,
			{
				"Transmit timeout", "can.err.tx_timeout",
				FT_BOOLEAN, 32,
				NULL, CAN_ERR_TX_TIMEOUT,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_lostarb,
			{
				"Lost arbitration", "can.err.lostarb",
				FT_BOOLEAN, 32,
				NULL, CAN_ERR_LOSTARB,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_ctrl,
			{
				"Controller problems", "can.err.ctrl",
				FT_BOOLEAN, 32,
				NULL, CAN_ERR_CTRL,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_prot,
			{
				"Protocol violation", "can.err.prot",
				FT_BOOLEAN, 32,
				NULL, CAN_ERR_PROT,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_trx,
			{
				"Transceiver status", "can.err.trx",
				FT_BOOLEAN, 32,
				NULL, CAN_ERR_TRX,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_ack,
			{
				"No acknowledgement", "can.err.ack",
				FT_BOOLEAN, 32,
				NULL, CAN_ERR_ACK,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_busoff,
			{
				"Bus off", "can.err.busoff",
				FT_BOOLEAN, 32,
				NULL, CAN_ERR_BUSOFF,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_buserror,
			{
				"Bus error", "can.err.buserror",
				FT_BOOLEAN, 32,
				NULL, CAN_ERR_BUSERROR,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_restarted,
			{
				"Controller restarted", "can.err.restarted",
				FT_BOOLEAN, 32,
				NULL, CAN_ERR_RESTARTED,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_reserved,
			{
				"Reserved", "can.err.reserved",
				FT_UINT32, BASE_HEX,
				NULL, CAN_ERR_RESERVED,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_lostarb_bit_number,
			{
				"Lost arbitration in bit number", "can.err.lostarb.bitnum",
				FT_UINT8, BASE_DEC,
				NULL, 0,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_ctrl_rx_overflow,
			{
				"RX buffer overflow", "can.err.ctrl.rx_overflow",
				FT_BOOLEAN, 8,
				NULL, 0x01,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_ctrl_tx_overflow,
			{
				"TX buffer overflow", "can.err.ctrl.tx_overflow",
				FT_BOOLEAN, 8,
				NULL, 0x02,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_ctrl_rx_warning,
			{
				"Reached warning level for RX errors", "can.err.ctrl.rx_warning",
				FT_BOOLEAN, 8,
				NULL, 0x04,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_ctrl_tx_warning,
			{
				"Reached warning level for TX errors", "can.err.ctrl.tx_warning",
				FT_BOOLEAN, 8,
				NULL, 0x08,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_ctrl_rx_passive,
			{
				"Reached error passive status RX", "can.err.ctrl.rx_passive",
				FT_BOOLEAN, 8,
				NULL, 0x10,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_ctrl_tx_passive,
			{
				"Reached error passive status TX", "can.err.ctrl.tx_passive",
				FT_BOOLEAN, 8,
				NULL, 0x20,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_ctrl_active,
			{
				"Recovered to error active state", "can.err.ctrl.active",
				FT_BOOLEAN, 8,
				NULL, 0x40,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_prot_error_type_bit,
			{
				"Single bit error", "can.err.prot.type.bit",
				FT_BOOLEAN, 8,
				NULL, 0x01,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_prot_error_type_form,
			{
				"Frame format error", "can.err.prot.type.form",
				FT_BOOLEAN, 8,
				NULL, 0x02,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_prot_error_type_stuff,
			{
				"Bit stuffing error", "can.err.prot.type.stuff",
				FT_BOOLEAN, 8,
				NULL, 0x04,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_prot_error_type_bit0,
			{
				"Unable to send dominant bit", "can.err.prot.type.bit0",
				FT_BOOLEAN, 8,
				NULL, 0x08,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_prot_error_type_bit1,
			{
				"Unable to send recessive bit", "can.err.prot.type.bit1",
				FT_BOOLEAN, 8,
				NULL, 0x10,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_prot_error_type_overload,
			{
				"Bus overload", "can.err.prot.type.overload",
				FT_BOOLEAN, 8,
				NULL, 0x20,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_prot_error_type_active,
			{
				"Active error announcement", "can.err.prot.type.active",
				FT_BOOLEAN, 8,
				NULL, 0x40,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_prot_error_type_tx,
			{
				"Error occurred on transmission", "can.err.prot.type.tx",
				FT_BOOLEAN, 8,
				NULL, 0x80,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_prot_error_location,
			{
				"Protocol error location", "can.err.prot.location",
				FT_UINT8, BASE_DEC,
				VALS(can_err_prot_error_location_vals), 0,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_trx_canh,
			{
				"Transceiver CANH status", "can.err.trx.canh",
				FT_UINT8, BASE_DEC,
				VALS(can_err_trx_canh_vals), 0x0F,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_trx_canl,
			{
				"Transceiver CANL status", "can.err.trx.canl",
				FT_UINT8, BASE_DEC,
				VALS(can_err_trx_canl_vals), 0xF0,
				NULL, HFILL
			}
		},
		{
			&hf_can_err_ctrl_specific,
			{
				"Controller specific data", "can.err.ctrl_specific",
				FT_BYTES, SEP_SPACE,
				NULL, 0,
				NULL, HFILL
			}
		},
	};

	/* Setup protocol subtree array */
	static gint *ett[] =
		{
			&ett_can,
			&ett_can_fd
		};

	static ei_register_info ei[] = {
		{
			&ei_can_err_dlc_mismatch,
			{
				"can.err.dlc_mismatch", PI_MALFORMED, PI_ERROR,
				"ERROR: DLC mismatch", EXPFILL
			}
		}
	};

	module_t *can_module;

	proto_can = proto_register_protocol("Controller Area Network", "CAN", "can");
	socketcan_bigendian_handle = register_dissector("can-bigendian", dissect_socketcan_bigendian, proto_can);
	socketcan_hostendian_handle = register_dissector("can-hostendian", dissect_socketcan_hostendian, proto_can);

	proto_canfd = proto_register_protocol("Controller Area Network FD", "CANFD", "canfd");
	socketcan_fd_handle = register_dissector("canfd", dissect_socketcan_fd, proto_canfd);

	proto_register_field_array(proto_can, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));

	expert_register_field_array(expert_register_protocol(proto_can), ei, array_length(ei));

	can_module = prefs_register_protocol(proto_can, NULL);

	prefs_register_obsolete_preference(can_module, "protocol");
	prefs_register_bool_preference(can_module, "byte_swap",
	    "Byte-swap the CAN ID/flags field",
	    "Whether the CAN ID/flags field should be byte-swapped",
	    &byte_swap);

	subdissector_table = register_decode_as_next_proto(proto_can, "Network", "can.subdissector", "CAN next level dissector", NULL);
}

void
proto_reg_handoff_socketcan(void)
{
	dissector_add_uint("wtap_encap", WTAP_ENCAP_SOCKETCAN, socketcan_bigendian_handle);
	dissector_add_uint("sll.ltype", LINUX_SLL_P_CAN, socketcan_hostendian_handle);
	dissector_add_uint("sll.ltype", LINUX_SLL_P_CANFD, socketcan_fd_handle);
}

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: t
 * End:
 *
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
 * :indentSize=8:tabSize=8:noTabs=false:
 */